CodeForces 551D GukiZ and Binary Operations DP+矩阵乘法

来源:互联网 发布:手机淘宝查看退货率 编辑:程序博客网 时间:2024/04/30 01:53

dp方程和fib数列一样。。

#include <cstdio>#include <cstring>#define rep(i,j,k) for(int i=j;i<k;i++)typedef long long ll;ll n, K, l, mod;struct Matrix {    ll v[2][2];    Matrix(ll x = 0) { rep(i,0,2)rep(j,0,2)v[i][j]=0;rep(i,0,2) v[i][i] = x; }    ll *operator [](int x) { return v[x]; }    friend Matrix operator *(Matrix a, Matrix b){        Matrix c;        rep(i,0,2) rep(j,0,2) rep(k,0,2)            c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;        return c;    }    friend Matrix operator ^(Matrix a, ll n){        Matrix ans(1);        for (; n; n >>= 1, a = a * a)            if (n & 1) ans = ans * a;        return ans;    }} b;ll quick_pow(ll a, ll n) {    ll ans = 1;    for (; n; n /= 2, a = a * a % mod)        if (n & 1) ans = ans * a % mod;    return ans;}int main() {    b[0][0]=b[0][1]=b[1][0]=1;    scanf("%I64d%I64d%I64d%I64d", &n, &K, &l, &mod);    if (l < 63 && 1ll << l <= K || mod == 1) { puts("0"); return 0; }    ll ans = 1, fib = (b ^ (n + 1))[0][0], pow2 = quick_pow(2, n), t = (pow2 - fib + mod) % mod;    for (int i = 0; i < l; i++)        if (((K >> i) & 1) == 0)            ans = ans * fib % mod;        else            ans = ans * t % mod;    printf("%I64d", ans);    return 0;}


D. GukiZ and Binary Operations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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!

Input

First and the only line of input contains four integers nklm (2 ≤ n ≤ 10180 ≤ k ≤ 1018,0 ≤ l ≤ 641 ≤ m ≤ 109 + 7).

Output

In 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 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}


0 0