CF 551DGukiZ and Binary Operations 矩阵/位运算

12 篇文章 0 订阅
7 篇文章 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 (inPascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation  means bitwise OR (in Pascal it is equivalent to , in C/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 ≤ 10180 ≤ 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}.


数组长度为n,, l是数位长度,m是取模数

满足的  每一个a都小于2^l 的数组数目是多少?


按某一位来看,如果k的某一位是1,那么必须满足至少相邻两个a的某一位都是1,

如果k的那位是0,则数组相邻的a那一位不全是1.

可以证得 k是0时,数组a的种类数随着长度的增加满足斐波那契数列。

于是题目可以变成计算F(n)的值,和2^n-F(n)     分别是k的某位为0和1的情况

由于N为1e18,需矩阵快速幂。

l为63时longlong会变负,64会溢出


PS : 模为1的时候答案一定为0!

#include<bits/stdc++.h>
#define For(i,a,b) for(int (i)=(a);(i) < (b);(i)++)
#define rof(i,a,b) for(int (i)=(a);(i) > (b);(i)--)
#define IOS ios::sync_with_stdio(false)
#define lson l,m,rt <<1
#define rson m+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int maxn = 2e5+10;
const int INF =0x3f3f3f3f;
int MOD;
ll n,k,l;
struct MATRIC{
    ll ele[5][5];
    MATRIC(){mem(ele,0);}
};
MATRIC mul ( ll n, MATRIC  m1, MATRIC m2)
{
    MATRIC ans;
    For(i,0,n){
        For(j,0,n){
            ll ret=0;
            For(k,0,n){
                ret=(ret+m1.ele[i][k]*m2.ele[k][j]%MOD)%MOD;
            }
            ans.ele[i][j]=ret;
        }
    }
    return ans;
}
MATRIC matric_mul_pow(MATRIC m1,ll b)
{
    MATRIC ans;
    For(i,0,2)
        For(j,0,2)
            if(i==j)ans.ele[i][j]=1;
    while(b){
        if(b&1){
            ans=mul(2,ans,m1);
        }
        b>>=1;
        m1=mul(2,m1,m1);
    }
    return ans;
}
ll pow2(ll b)
{
    ll ans=1,a=2;
    while(b){
        if(b&1) ans=(ans*a)%MOD;
        b>>=1;
        a=a*a%MOD;
    }
    return ans;
}
int main()
{
    cin>>n>>k>>l>>MOD;
    MATRIC m;
    ll y=1;
    if(l<63){
        y<<=l;
        if(y<=k) {

            cout<<0<<endl;
            return 0;
        }
    }
    m.ele[0][0]=0;m.ele[0][1]=1;m.ele[1][0]=1;m.ele[1][1]=1;
    m=matric_mul_pow(m,n);
    ll x=(m.ele[0][1]+m.ele[1][1])%MOD;
    y=(pow2(n)-x+MOD)%MOD;

    ll ans=1;
    for(int i=0;i<l;i++){
        if(i==63){
            ans=(ans*x)%MOD;
            break;
        }
        if(k&1) ans=(ans*y)%MOD;
        else ans=(ans*x)%MOD;
        k>>=1;
    }
    //cout<<x<<endl<<y<<endl;
    cout<<ans%MOD<<endl;

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值