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

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}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值