GukiZ and Binary Operations(矩阵+二进制)

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 ≤ 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}.

 

 

题意:你可以任意挑选小于2^l的n个数,让它们以这个公式 ,两两取与再取或的方式最后答案为k,问你有多少种方案数,答案取余m
思路:这题看了别人的题解之后终于明白了。首先,我们把k转换为二进制来看,若某一位为1,则必须存在n个数中至少有相邻的两个数那一位都为1,若某一位为0,组必须存在n个数它们不能有相邻的两个数那一位都为1.这样我们相当于求k每一位在n个数字中的方案数,答案是每一位的方案数相乘起来。

注意l=64的时候,要特别注意一下

 

我用了无符号的long long 各种错。。。最后还是long long 过的。

不知道是不是我编译器坏了。

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL  long long
using namespace std;//unsigned
struct 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 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);
    //相邻没有连续两个1
    LL x=(ans.mat[0][0]+ans.mat[0][1])%mod;
    //至少有一个连续两个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;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值