2022 - 2023, Shandong University of Science and Technology, bachelor’s degree in Math. 2023 - 2026, transform my bachelor’s degree to Computer Science.
What is the minimun complexity to find n’th Fibonacci Number?
We can find n’th Fibonacci Number in O(log n) time using $Matrix\quad Exponentiation$. In this post, a general implementation of Matrix Exponentiation is discussed.
For solving the problem, we are assuming a linear recurrence eqution like below:
F(n) = a * F(n - 1) + b * F(n - 2) + c * F(n - 3) for n >= 3 ....Equation (1)
where a, b, c are constants.
For this recurrence relation, it depends on three previous value.
Now we will try to represent Equation(1) in terms of the matrix.
|F(n)| |F(n - 1)|
|F(n - 1)| = Matrix 'C' * |F(n - 2)|
|F(n - 2)| |F(n - 3)|
Now we need to fill the Matrix 'C'.
so according to our equation.
|a b c|
Matrix 'C' = |1 0 0|
|0 1 0|
so for n, the equation(1) changes to
|F(n)| |F(2)|
|F(n - 1)| = C ^ (n - 2) * |F(1)|
|F(n - 2)| |F(0)|
So we can simply multiply our matrix 'C' n - 2 times and the multiply it with the matrix below to get the result.
|F(2)|
|F(1)|
|F(0)|
Multiplication can be done in O(Log n) time using Divie and Conquer algorithm for fast power.
There is rough code below.
typedef struct Mat{
int m[105][105];
}Mat;
Mat a;
Mat multiply(Mat x, Mat y){
// Creating an new matrix to store elements
// of the multiplication matrix
Mat c;
for(int i = 1; i <= 3; ++ i){
for(int j = 1; j <= 3; ++ j){
c.m[i][j] = 0;
for(int k = 1; k <= 3; ++ k)
c.m[i][j] += x.m[i][k] * y.m[k][j];
}
}
return c;
// return the result
}
Mat Matrix_fastpow(Mat a, Mat k){
//initial values for matrix 'C'
Mat res;
res.m[1][1] = a;res.m[1][2] = b;res.m[1][3] = c;
res.m[2][1] = 1;res.m[2][2] = 0;res.m[2][3] = 0;
res.m[3][1] = 0;res.m[3][2] = 1;res.m[3][3] = 0;
while(k){
if(k & 1){
res = multiple(a, res);
}
k >>= 1;
a = multiple(a, a);
}
return res;
}
ListNode* reverseList(ListNode* L){ //如果没有头节点 需要建立头节点 ListNode * head = (ListNode *)malloc(sizeof (ListNode)); head -> next = L;
//如果已有头节点 auto p = head -> next; head -> next = NULL; while(p){ auto t = p -> next; // 暂存p下一节点 p -> next = head -> next; head -> next = p; p = t; } return head -> next; }
尾插法建立单链表
1 2 3 4 5 6 7 8 9 10
LinkList L = (LNode *)malloc(sizeof (LNode)); int n;cin>>n; for(int i = 0; i < n; ++ i){ int t;cin>>t; auto p = (LNode *)malloc(sizeof (LNode)); p -> val = t; p -> next = L ->next; L -> next = p; }
递归建立单链表
1 2 3 4 5 6 7 8 9 10 11
voidcreat(LinkList L,int n){ if(!n)return; else{ creat(L, n - 1); int t;scanf("%d", &t); LNode *p = (LNode *)malloc(sizeof (LNode)); p -> val = t; p -> next = L -> next; L -> next = p; } }
双指针求倒数第k项
1 2 3 4 5 6 7 8 9 10 11 12
intget_k_LinkList(LinkList L, int k){ LNode *fast = L, *slow = L; for(int i = 0; i < k; ++ i){ fast = fast -> next; } while(fast){ fast = fast -> next; slow = slow -> next; } return slow -> data; }
题目链接 无向图从1到n存在m条light trains m>=n-1 所以点1和n一定是联通的 存在k个wormhole 可以随机传送到另外k-1个位置 且at most once 求最少经过多少条light trains 可以到达点n 思路
• Compute dist(s, w) and dist(w, t) for each wormhole w and sum S using two BFS from s and t. • Determine the wormhole you should enter to minimize the expected distance. • If you enter wormhole w , the expected distance from s to t is $$dist_w=dist(s,w)+\frac{1}{k-1}\sum dist(w_1,t)(w_1≠w)=dist(s,w)+\frac{1}{k-1}(S-dist(w_1,t))\with\quad S=\sum dist(w_1,t)$$
int a[200005]; int mp[1000005]; set<int>st; int ma; signedmain(){ int n=read(); for(int i=1;i<=n;i++){ a[i]=read(); mp[a[i]]=1; ma=max(ma,a[i]); } int q=read(); for(int i=1;i<=ma+q;i++) { if(!mp[i])st.insert(i); } for(int i=0;i<q;i++){ int tt=read(); auto t=st.upper_bound(a[tt]); int b=*t; cout<<b<<endl; st.erase(t);//删掉空座t st.insert(a[tt]);//添加产生的新空座 注意该青蛙后面的frog可以到达这个位置 a[tt]=b;//更新跳跃青蛙的位置 } }