标签:d. gukiz and binary cf codeforces round #30
We all know that GukiZ often plays with arrays.
Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: ? Here operation means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation means bitwise OR (in Pascal it is equivalent to , inC/C++/Java/Python it is equivalent to |).
Because the answer can be quite large, calculate it modulo m. This time GukiZ hasn‘t come up with solution, and needs you to help him!
First and the only line of input contains four integers n, k, l, m (2?≤?n?≤?1018, 0?≤?k?≤?1018, 0?≤?l?≤?64, 1?≤?m?≤?109?+?7).
In the single line print the number of arrays satisfying the condition above modulo m.
2 1 2 10
3
2 1 1 3
1
3 3 2 10
9
In the first sample, satisfying arrays are {1,?1},?{3,?1},?{1,?3}.
In the second sample, only satisfying array is {1,?1}.
In the third sample, satisfying arrays are{0,?3,?3},?{1,?3,?2},?{1,?3,?3},?{2,?3,?1},?{2,?3,?3},?{3,?3,?0},?{3,?3,?1},?{3,?3,?2},?{3,?3,?3}.
注意l=64的时候,要特别注意一下
我用了无符号的long long 各种错。。。最后还是long long 过的。
不知道是不是我编译器坏了。
#include<stdio.h> #include<string.h> #include<algorithm> #define LL long long using namespace std;//unsigned struct matrix { LL mat[2][2]; }; LL mod; matrix multiply(matrix a,matrix b) { matrix c; memset(c.mat,0,sizeof(c.mat)); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { if(a.mat[i][j]==0)continue; for(int k=0;k<2;k++) { if(b.mat[j][k]==0)continue; c.mat[i][k]+=a.mat[i][j]*b.mat[j][k]%mod; // c.mat[i][k]%=mod; if(c.mat[i][k]>mod) c.mat[i][k]-=mod; else if(c.mat[i][k]<0) c.mat[i][k]+=mod; } } } return c; } matrix quicklymod(matrix a,LL n) { matrix res; memset(res.mat,0,sizeof(res.mat)); for(int i=0;i<2;i++) res.mat[i][i]=1; while(n) { if(n&1) res=multiply(a,res); a=multiply(a,a); n>>=1; } return res; } LL ppow(LL a,LL b) { LL c=1; while(b) { if(b&1) c=c*a%mod; b>>=1; a=a*a%mod; } return c; } int main() { LL n,k,l,m; scanf("%I64d%I64d%I64d%I64d",&n,&k,&l,&mod); if(l!=64&&k>=(unsigned long long )(1ULL<<l)){printf("0\n");return 0;} matrix ans; ans.mat[0][0]=1;ans.mat[0][1]=1; ans.mat[1][0]=1;ans.mat[1][1]=0; ans=quicklymod(ans,n); //相邻没有连续两个1 LL x=(ans.mat[0][0]+ans.mat[0][1])%mod; //至少有一个连续两个1 LL y=((ppow(2,n)-x)%mod+mod)%mod; // printf("x=%I64d\ty=%I64d\n",x,y); LL sum=1; for(LL i=0;i<l;i++) { if(k&(1LL<<i)) sum=(sum*y)%mod; else sum=sum*x%mod; } printf("%I64d\n",sum%mod); return 0; }
GukiZ and Binary Operations(矩阵+二进制)
标签:d. gukiz and binary cf codeforces round #30
原文地址:http://blog.csdn.net/u010579068/article/details/46522829