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

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

思路:首先看到或,并就想将这个数拆开为二进制的01串,分别考虑每一位的0,1;

   当前k的那个位置为0时,表示a1-an中没有两个相邻的1;

   同理,当前k为为1时,表示a1-an中有两个相邻的1;2^n,减去0的方案即是;

   刚刚开始一直在想组合数学的求法,发现不好写(。。。我也不会)

   后来发现dp可以做,但是n很大;

   dp方程:dp[i][0]=dp[i-1][1]+dp[i-1][0];

       dp[i][1]=dp[i-1][0];

   dp[i][j]表示第i位为j的无相邻1的方案数;

   乍一看很像斐波那契,构造矩阵;

                 [  1  ,   1   ]

   [ dp[i-1][0] , dp[i-1][1] ]  *[  1  ,   0   ]     =[   dp[i][0]   ,   dp[i][1]   ];

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x,y) cout<<"bug"<<x<<" "<<y<<endl;
#define bug(x) cout<<"xxx "<<x<<endl;
const int N=1e5+,M=1e6+,inf=2e9+,mod=1e9+;
const ll INF=1e18+;
ll MOD;
struct Matrix
{
ll a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%MOD;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%MOD;
return C;
}
Matrix operator ^ (const ll &t)const
{
Matrix A=(*this),res;
res.init();
ll p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
ll quickmod(ll a,ll b,ll c)
{
ll ans=;
while(b)
{
if(b&)ans=(ans*a)%c;
b>>=;
a=(a*a)%c;
}
return ans;
}
int main()
{
ll n,k,m,l;
cin>>n>>k>>l>>m;
MOD=m;
Matrix base,ans;
base.a[][]=base.a[][]=base.a[][]=;
base.a[][]=;
ans.a[][]=ans.a[][]=;
ans.a[][]=ans.a[][]=;
ans=ans*(base^(n-));
ll zero=(ans.a[][]+ans.a[][])%m;
ll one=((quickmod(2LL,n,m)-zero)%m+m)%m;
//cout<<zero<<" "<<one<<endl;
ll out=;
if((l<=&&k>=(1LL<<l)))return puts("");
for(ll i=l-;i>=;i--)
{
if(i>)
out*=zero;
else
{
ll x=(1LL<<i)&k;
if(x)
out*=one;
else
out*=zero;
}
out%=m;
}
printf("%lld\n",out%m);
return ;
}

Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp的更多相关文章

  1. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

  2. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations

    得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...

  3. Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)

    Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...

  4. 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest

    题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...

  5. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块

    E. GukiZ and GukiZiana Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...

  6. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分

    C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  7. Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题

    A. GukiZ and Contest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  8. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 二分

    C. GukiZ hates Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)

    E. GukiZ and GukiZiana time limit per test 10 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. 【BZOJ3935】Rbtree 树形DP

    [BZOJ3935]Rbtree Description 给定一颗 N 个点的树,树上的每个点或者是红色,或者是黑色. 每个单位时间内,你可以任选两个点,交换它们的颜色. 出于某种恶趣味,你希望用最少 ...

  2. SpringBoot实现热加载方式

    一. spring-boot-devtools方式1.在pom.xml中加入以下代码: 2.标识红线的地方加上 3.在设置里面加上自动编译 4.shift+ctrl+alt+/ 这样就可以了! 二.s ...

  3. 徐州网络赛G-Trace【线段树】

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy  ...

  4. 使用Dataset构建数据到lgb中

    训练数据要放到Dataset中供lgb使用,构建数据如下: import lightgbm as lgb import numpy as np # 训练数据,500个样本,10个维度 train_da ...

  5. 使用or展开进行sql优化(即sql语法union all代替or可以提高效率)

    问题: 这样一条sql应该怎么优化? select * from sys_user where user_code = 'zhangyong' or user_code in (select grp_ ...

  6. Nginx服务基础

    Nginx的英文官方网站是http://nginx.org,在这里可以查看Nginx的各个软件版本信息.Nginx软件有三种版本:稳定版.开发版和历史稳定版.开发版更新较快,包含最新的功能和bug的修 ...

  7. Wormholes---poj3259(最短路 spfa 判断负环 模板)

    题目链接:http://poj.org/problem?id=3259 题意是问是否能通过虫洞回到过去: 虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts. 我们把虫洞看成是一条负权路,问 ...

  8. 点击劫持漏洞解决( Clickjacking: X-Frame-Options header missing)

    点击劫持漏洞 X-Frame-Options HTTP 响应头, 可以指示浏览器是否应该加载一个 iframe 中的页面. 网站可以通过设置 X-Frame-Options 阻止站点内的页面被其他页面 ...

  9. mysql 数据操作 多表查询 准备

    为什么需要多表查询: 因为我们不可能把所有数据都放在一张表里 我们把不同数据存储 放在一张一张不同表 方便管理,但我们为了方便管理,把数据拆分到一张一张表去存储. 但是数据还是一个整体,数据之间是有关 ...

  10. python 异常处理、进程

    目录: 异常处理 python进程 python并发之多进程 一.异常处理(try...except...) 1.程序中难免出现错误,而错误分成两种: a.语法错误: b.逻辑错误(逻辑错误) 2.异 ...