D. GukiZ and Binary Operations (matrix + Binary), gukizbinary
D. GukiZ and Binary Operations
We all know that GukiZ often plays with arrays.
Now he is thinking about this problem: how many arraysA, Of lengthN, With non-negative elements strictly less then 2LMeet the following condition :? Here operation means bitwise AND (in Pascal it is equivalent to and, in C/C ++/Java/Python it is equivalent &), 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 moduloM. This time GukiZ hasn' t come up with solution, and needs you to help him!
Input
First and the only line of input contains four integersN,K,L,M(2 cores ≤ CoresNLimit ≤ limit 1018, 0 limit ≤ limitKLimit ≤ limit 1018, 0 limit ≤ limitLLimit ≤ limit 64, 1 limit ≤ limitMLimit ≤ limit 109 Limit + limit 7 ).
Output
In the single line print the number of arrays satisfying the condition abve moduloM.
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 are {1, Region 1}, region {3, Region 1}, region {1, Region 3 }.
In the second sample, only satisfying array is {1, interval 1 }.
In the third sample, satisfying arrays are {0, numbers 3, numbers 3}, numbers {1, numbers 3, numbers 2}, numbers {1, numbers 3, numbers 3 }, token {2, Token 3, Token 1}, Token {2, Token 3, Token 3}, Token {3, Token 3, Token 0}, Token {3, Token 3, token 1}, Token {3, Token 3, token 2}, Token {3, Token 3, Token 3 }.
Q: You can select n numbers smaller than 2 ^ l and use this formula to obtain and reconnect to OR. The final answer is k. How many solutions do you have, the answer follows the concept of m: After reading other people's questions, I finally understood them. First, we convert k to binary. If a certain bit is 1, there must be at least two adjacent numbers in n numbers, if one digit is 0, the Group must have n numbers. They cannot have two adjacent numbers. in this way, we calculate the number of solutions for each k digit in n numbers. The answer is that the number of solutions for each digit is multiplied.
Pay special attention to l = 64.
I use unsigned long for various errors... The last is long.
I don't know if my compiler is broken.
Reprinted, please specify the Source: search for & STAR kids
Link: http://codeforces.com/contest/551/problem/D
1 # include <stdio. h> 2 # include <string. h> 3 # include <algorithm> 4 # define LL long 5 using namespace std; // unsigned 6 struct matrix 7 {8 LL mat [2] [2]; 9 }; 10 LL mod; 11 12 matrix multiply (matrix a, matrix B) 13 {14 matrix c; 15 memset (c. mat, 0, sizeof (c. mat); 16 for (int I = 0; I <2; I ++) 17 {18 for (int j = 0; j <2; j ++) 19 {20 if (. mat [I] [j] = 0) continue; 21 for (int k = 0; k <2; k ++) 22 {23 if (B. mat [j] [k] = 0) continue; 24 C. mat [I] [k] + =. mat [I] [j] * B. mat [j] [k] % mod; 25 // c. mat [I] [k] % = mod; 26 if (c. mat [I] [k]> mod) c. mat [I] [k]-= mod; 27 else if (c. mat [I] [k] <0) c. mat [I] [k] + = mod; 28} 29} 30} 31 return c; 32} 33 34 matrix quicklymod (matrix a, LL n) 35 {36 matrix res; 37 memset (res. mat, 0, sizeof (res. mat); 38 for (int I = 0; I <2; I ++) res. mat [I] [I] = 1; 39 while (n) 40 {41 if (n & 1) 42 res = multiply (a, res ); 43 a = multiply (a, a); 44 n >=1; 45} 46 return r Es; 47} 48 49 LL ppow (LL a, LL B) 50 {51 LL c = 1; 52 while (B) 53 {54 if (B & 1) c = c * a % mod; 55 B >>= 1; 56 a = a * a % mod; 57} 58 return c; 59} 60 61 62 int main () 63 {64 LL n, k, l, m; 65 scanf ("% I64d % I64d % I64d % I64d", & n, & k, & l, & mod ); 66 if (l! = 64 & k> = (unsigned long) (1ULL <l) {printf ("0 \ n"); return 0 ;}67 matrix ans; 68 ans. mat [0] [0] = 1; ans. mat [0] [1] = 1; 69 ans. mat [1] [0] = 1; ans. mat [1] [1] = 0; 70 ans = quicklymod (ans, n); 71 // two adjacent 172 LL x = (ans. mat [0] [0] + ans. mat [0] [1]) % mod; 73 // at least one 174 LL y = (ppow (2, n)-x) % mod + mod) in a row) % mod; 75 // printf ("x = % I64d \ ty = % I64d \ n", x, y); 76 LL sum = 1; 77 for (LL I = 0; I <l; I ++) 78 {79 if (k & (1LL <I) sum = (sum * y) % mod; 80 else sum = sum * x % mod; 81} 82 printf ("% I64d \ n", sum % mod); 83 return 0; 84}
Others' unsigned ones...
1 # include <iostream> 2 # include <cstdio> 3 # include <string> 4 # include <cstring> 5 # include <vector> 6 # include <cmath> 7 # include <queue> 8 # include <stack> 9 # include <map> 10 # include <set> 11 # include <algorithm> 12 using namespace std; 13 typedef unsigned long LL; 14 ll n, K, L, MOD; 15 struct Matrix16 {17 LL mat [2] [2]; 18 Matrix () {memset (mat, 0, sizeof (mat);} 19 Matrix operator * (Matrix A) 20 {21 Matrix res; 22 For (int I = 0; I <2; I ++) 23 for (int j = 0; j <2; j ++) 24 for (int k = 0; k <2; k ++) 25 res. mat [I] [j] = (res. mat [I] [j] + mat [I] [k] *. mat [j] [k] % MOD) % MOD; 26 return res; 27} 28}; 29 LL pow_mul (LL x, LL n) 30 {31 LL res = 1; 32 while (n) 33 {34 if (n & 1) res = (res * x) % MOD; 35 x = (x * x) % MOD; 36 n> = 1; 37} 38 return res; 39} 40 Matrix matrix_pow_mul (Matrix A, LL n) 41 {42 Matrix res; 43 for (int I = 0; I <2; I ++) res. mat [I] [I] = 1; 44 while (n) 45 {46 if (n & 1) Res = res * A; 47 A = A * A; 48 n> = 1; 49} 50 return res; 51} 52 int main () 53 {54 while (cin> N> K> L> MOD) 55 {56 if (L! = 64 & K> = (1ULL <L) {printf ("0 \ n"); continue;} 57 Matrix A, B; 58. mat [0] [0] =. mat [0] [1] =. mat [1] [0] = 1; 59 A = matrix_pow_mul (A, N-2); 60 B. mat [0] [0] = 3; 61 B. mat [0] [1] = 2; 62 A = A * B; 63 LL ans = 1; 64 LL sum = pow_mul (2, N ); 65 for (LL I = 0; I <L; I ++) 66 {67 if (K & (1LL <I )) ans = (ans * (sum-A.mat [0] [0] + MOD) % MOD; 68 else ans = (ans *. mat [0] [0]) % MOD; 69} 70 cout <ans % MOD <endl; 71} 72 return 0; 73}View Code
I use dp [I] [j] to represent the number of I, and j to indicate that the number of solutions with no adjacent values of 1 is met when the last number is 0 or 1, because n> = 2, therefore, I is meaningful only when I is greater than 2. First, dp [1] [0] = 1, dp [1] [1] = 1, dp [2] [0] = dp [1] [0] + dp [1] [1], dp [2] [1] = dp [1] [0] ...... The general formula is "dp [n] [0] = dp [n-1] [0] + dp [n-1] [1]. dp [n] [1] = dp [n-1] [0], which means that when the last number of n is 0, if the last digit is 1, we can only fill in 1 when the last bit of N-1 is 0, so that there will be no adjacent 1. Finally, you need to calculate all [n] = dp [n] [0] + dp [n] [1], dp [n] [0] = all [n-1], dp [n] [1] = dp [n-1] [0] = all [N-2], the introduction of all [n] = all [n-1] + all [N-2], this is the Fibonacci series. However, the initial values are somewhat different. The values of all [n] = fib [n + 1] and fib 0th and 1st are 1. after calculating the non-adjacent scheme, you only need to calculate the total number of schemes 2 ^ n (0 for each bit or 1) minus the number of non-adjacent schemes, that is, the adjacent scheme. You can also use dp to push it. I will mention it a little. I use c [n] to indicate the number of solutions with adjacent 1 when the length is n. c [2] = 1, c [3] = c [2] * 2 + dp [2] [1] ...... C [n] = c [n-1] * 2 + dp [n-1] [1] = c [n-1] * 2 + all [n-3] Here dp is obtained above there is no adjacent of 1, since c [n-1] has an adjacent 1, the next digit is arbitrary. dp [n-1] [1] is the last digit with the length of N-1 as 1, let's add 1 so that it has adjacent 1. after the Fibonacci sequence is obtained, we can use the Matrix to quickly solve the Fibonacci number by using the power of the matrix, or use the Matrix to quickly calculate the power of c. Note: There are many wa points in this question, first, unsigned long is used to determine whether the request is out of bounds, and the number of fast power is long. When enumerating l bits, the 63rd BITs have been violent for 10 ^ 18, therefore, special judgment is required. The final output result must be % mod. Otherwise, it has a sample with mod 1 and l = 0.View Code