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

且0时其方法总数为f(n) = f(n-1) + f(n-2),其中f(2) = 3,f(1) = 3。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,k;
int l,m;
unsigned long long p[];
queue<int> q;
//0: f[n] = f[n-2] + f[n-1]
//1: 2^i - f[n]
struct matrix{
ll a[][];
matrix(){
a[][] = a[][] = a[][] = a[][] = ;
}
void unit(){
a[][] = a[][] = ;
}
matrix operator * (const matrix& p){
matrix ans;
for(int i = ;i < ;++i){
for(int j = ;j < ;++j){
for(int k = ;k < ;++k){
ans.a[i][j] += a[i][k] * p.a[k][j];
if(ans.a[i][j] >= m) ans.a[i][j] %= m;
}
}
}
return ans;
}
};
ll fi(){
//f[1] = 2,f[2] = 3;
if(n == ) return ;
if(n == ) return ;
ll t = n;
t -= ;
matrix ans,p;
p.a[][] = ,p.a[][] = ,p.a[][] = ;
ans.unit();
while(t){
if(t & ) ans = ans * p;
p = p * p;
t >>= ;
}
return ( * ans.a[][] + * ans.a[][])%m;
}
ll quickpow(ll x,ll y){
ll ans = ;
while(y){
if(y & ){
ans = ans * x;
if(ans >= m) ans %= m;
}
x *= x;
if(x >= m) x %= m;
y >>= ;
}
return ans;
}
void solve(){
if(p[l] - < (unsigned long long)k && l != ){
puts("");
return;
}
while(k){
q.push(k&);
k>>=;
}
ll x = fi(),y = (quickpow(,n) - x + m) % m,ans = ;
for(int i = ;i < l;++i){
if(!q.empty()){
if(q.front()) ans = ans * y;
else ans = ans * x;
q.pop();
}
else{
ans = ans * x;
}
if(ans >= m) ans %= m;
}
printf("%I64d\n",ans%m);
}
int main()
{
cin >> n >> k >> l >> m;
p[] = ;
for(int i = ;i < ;++i) p[i] = p[i-]*;
solve();
return ;
}

Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations的更多相关文章

  1. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

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

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

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

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

  4. 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 ...

  5. 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/ ...

  6. 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/ ...

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

  8. 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 ...

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

    题目地址:http://codeforces.com/contest/551/problem/E 将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个总体偏移量. 改动操作:l~r区间内,对 ...

随机推荐

  1. HTTP协议详解-基础知识

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.绝大多数的Web开发,都是构建在HTTP协议之上的Web应用. HTTP协议的主要特点可概括如下: 简单: ...

  2. JS设置组合快捷键

    为提升用户体验,想要在web页面中通过组合快捷键调出用户帮助页面,具体实现思路是监听keyup事件,在相应的处理函数中进行逻辑编写,代码如下 $(document).keyup(function (e ...

  3. docker:安装redis

    文章来源:https://www.cnblogs.com/hello-tl/p/9239474.html 1.添加镜像 # docker pull redis:4.0 2.在/data下新建文件夹re ...

  4. 数据结构( Pyhon 语言描述 ) — —第9章:列表

    概念 列表是一个线性的集合,允许用户在任意位置插入.删除.访问和替换元素 使用列表 基于索引的操作 基本操作 数组与列表的区别 数组是一种具体的数据结构,拥有基于单个的物理内存块的一种特定的,不变的实 ...

  5. 杭电 1159 Common Subsequence

    Problem Description A subsequence of a given sequence is the given sequence with some elements (poss ...

  6. 合肥工业大学数据结构上机实验代码与实验报告(全)github地址

    我已经将这个学期的所有数据结构上机实验的代码与报告上传到github上了,一直都有这个想法,但没抽出时间来学习git.经过上周简单的练习后,我已经基本学会运营自己的代码仓库了.所有代码都是C++写的类 ...

  7. SVN如何避免冲突

    在团队开发时,必然会用到代码版本控制工具,比如SVN. 但是多人共同维护同一份代码,当对同一文件进行增删时,就可能造成冲突,如何尽可能避免冲突相当重要. 首先,每次,新建任何文档,都会修改项目文件,所 ...

  8. java 中 image 和 byte[] 相互转换

      java 中 image 和 byte[] 相互转换可恶的…………其实也挺好的 只是把好不容易写出来的东西记下来,怕忘了…… 下面,我来介绍一个简单的 byte[] to image, 我们只需要 ...

  9. Python paramiko模块 + 堡垒机

    paremiko SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: import paramiko    # 创建SSH对象 ssh = paramiko.SSHClient ...

  10. appium之android_uiautomator定位

    前言 appium就是封装android的uiautomator这个框架来的,所以uiautomator的一些定位方法也可以用 text 1.通过text文本定位语法 new UiSelector() ...