D. Gukiz and Binary operationstime limit per test1 secondmemory limit per test256 megabytesinputstandard Inputoutputstanda RD output
We all know this gukiz often plays with arrays.
Now he's 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's equivalent to and, in C/c++/java/python It's equivalent to &), Operation Means bitwise OR (in Pascal It's equivalent to , inC/c++/java/python It's equivalent to |).
Because the answer can be quite large, calculate it modulo m. This time Gukiz hasn ' t come up with solution, and needs your to help him!
Input
First and the only line of input contains four integers N, k, L, m (2?≤? n. ≤?10 , 0?≤? k. ≤?10 , 0?≤? l. ≤?64, 1?≤? M.≤?109?+?7 ).
Output
The the single line print the number of arrays satisfying the condition above modulo m.
Sample Test (s) input
2 1 2 10
Output
3
Input
2 1 1 3
Output
1
Input
3 3 2 10
Output
9
Note
In the first sample, satisfying arrays is {1,?1},?{ 3,?1},? {1,?3}.
In the second sample, only satisfying array is {1,?1}.
In the third sample, satisfying arrays is{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}.
Test instructions: You can arbitrarily pick the number of n less than 2^l, let them with this formula, 22 take and re-fetch or the way the final answer is K, ask you how many kinds of program number, the answer to take more than M thinking: this question to see someone else's solution after finally understand. First, we convert K to binary, if one is 1, then there must be at least two digits in the n number that are 1, if one is 0, Group must have n number they cannot have adjacent two digits that one is 1. So we're going to be the equivalent of asking K for each of the number of solutions in n numbers, and the answer is to multiply the number of scenarios per bit.
When you pay attention to l=64, pay special attention.
I use unsigned long long all kinds of wrong ... And finally a long long.
I don't know if it's my compiler that's broken.
#include <stdio.h> #include <string.h> #include <algorithm> #define LL long longusing namespace std;// Unsignedstruct 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) (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); No contiguous two X 1 LL x= (ans.mat[0][0]+ans.mat[0][1])%mod; At least one continuous two x 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 (Matrix + binary)