I took this trick in the mock competition in the morning, and then I didn’t know it, so I decided to post.
Consider a problem of finding the shortest interval, which satisfies monotonicity (two-pointers can be used), insertion is fast, but deletion is very slow, it is possible to quickly judge whether the combination of two pieces of information can meet the conditions.
At this time, the ordinary ruler method cannot be used.
O(n+m) Algorithm implementation (the previous board has been deleted for a better reading experience):
const int p=1000000007; int fpm(int a,int b){ int c=1; for(;b;b>>=1,a=a*a%p)if(b&1)c=c*a%p; return c; } int f[1000005][2]; signed main(){ freopen("game.in","r",stdin); freopen("game.out","w",stdout); int n,m; read(n,m); if(n>m)swap(n,m); if(n==1) return write(fpm(2,m),'\n'); if(n==2)return write(4*fpm(3,m-1)%p,'\n'); if(n==3 && m==3)return write("112\n"); int ans=4* fpm(4,n-2)%p*fpm(3,m-n)%p*fpm(2,n-1)%p; if(n==3) ans=(ans+4*4*fpm(3,m-4)%p*fpm(2,n-1))%p; else ans=(ans+4*5*fpm(4,n-4)%p*fpm(3,m-n)%p*fpm(2,n-1))%p; f[3][0]=1; for(int i=4;i<=m;++i){ f[i][0]=f[i-1][0]; if(i<=n){ f[i][1]=(f[i-1][1]*4+f[i-2][0]*20)%p; } else if(i==n+1){ f[i][1]=(f[i-1][1]*3+f[i-2][0]*16)%p; } else f[i][1]=(f[i-1][1]*3+f[i-2][0]*12)%p; } if(n==m) ans=(ans+4*(f[n][0]*3+f[n][1]*2+f[n-1][0]*12)%p*fpm(2,n-2))%p; else ans=(ans+2* ((f[n][0]*4+f[n][1]*3+f[n-1][0]*16)*fpm(3,m-n-1)%p*fpm(2,n-1)+ (f[m][0]*3+f[m][1]*2+f[m-1][0]*9)*fpm(2,n-2))%p )%p; write(ans,'\n'); return 0; } O(log n + log m): (notice fn,0 = [n ≥ 3] , and then you can speed up the power quickly)
Code only
Given an array(array) and a number(sum), find the two numbers from the array that sums up the given number.
Example:
array : [1,2,6,5,8,9,17,10] , sum : 12
Answer for above example would be [2,10] , if no such numbers found return [-1,-1].
The approach to solve the given problem is to loop through the given array twice and make a simple check. If the sum of the numbers (first & second loop) is equal to the given sum, the code snippet below will make it clearer.
Problem Given an array(array) and a number(sum), find the two numbers from the array that sums up the given number.
Example: array : [1,2,6,5,8,9,17,10] , sum : 12
Answer for above example would be [2,10], if no such numbers found return [-1,-1].
The approach to solve the given problem is to loop through the given array twice and make a simple check. If the sum of the numbers (first & second loop) is equal to the given sum, the code snippet below will make it clearer.
The code that follows was written before. To do it, I created a straightforward linked list + insert using a queued linked list and then sorting it was my original plan, but it proved to be too complicated.
Simply insert the sort so that the tail is very slight. It serves no purpose and, when used in the tree list, appears more NC. Actually, it is better to use heap or qsort after all input has been received, and to dynamically allocate enough space based on the input n.