天天看點

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: 

GukiZ and Binary Operations(矩陣+二進制)

? Here operation 

GukiZ and Binary Operations(矩陣+二進制)

 means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation 

GukiZ and Binary Operations(矩陣+二進制)

 means bitwise OR (in Pascal it is equivalent to 

GukiZ and Binary Operations(矩陣+二進制)

, 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 n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ 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個數,讓它們以這個公式

GukiZ and Binary Operations(矩陣+二進制)

,兩兩取與再取或的方式最後答案為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;
}