Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations
得到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的更多相关文章
- 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 ...
 - Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
		
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
 - 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest
		
题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...
 - 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 ...
 - 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/ ...
 - 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/ ...
 - 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 ...
 - 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 ...
 - Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
		
题目地址:http://codeforces.com/contest/551/problem/E 将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个总体偏移量. 改动操作:l~r区间内,对 ...
 
随机推荐
- 题解 CF440A 【Forgotten Episode】
			
博客阅读更好 虽然这道题是紫题,但实际难度应该是橙题吧 首先,看到标签…… 紫题?但题目也太…… 这道题教会我们不要看标签 好了,废话少说,看到楼下许多大佬都用了数组,但我觉得可以不用 为什么? 我也 ...
 - python之list [ 列表 ]
			
1. 列表是什么? list [ ] 逗号隔开 是一个容器 可以存放任意类型 列表 == 书包 书包里可以放水杯.衣服.袜子.钱包 钱包里可以放钱.身份证件,可以包套包 2. 列表能干什么? 存储大量 ...
 - SpringBoot入门,新建SpringBoot项目
			
一.在Spring Initializr中创建初始化项目 https://start.spring.io/ 二.通过maven导入Idea中(解压后的项目) 解压文件 黄色的为项目需要的真正的代码 , ...
 - VS打包软件部署------ClickOnce应用安装   (各版本.net引导文件安装,再发布文档离线安装下载地址)
			
一.1.其他引导程序包:地址 https://msdn.microsoft.com/zh-cn/vstudio/bb898654.aspx 2.离线安装各版本.net下载版(各种语言):https: ...
 - xheditor的参数配置详解
			
2.2. 初始化参数列表 2.3. API函数接口列表 2.4. 上传程序开发规范 2.5. 插件开发指南 2.6. 皮肤设计指南 2.2. 初始化参数列表 初始化参数示例代码: $('#elm1') ...
 - Bootstrap历练实例:输入框组的大小
			
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
 - Java异常 Exception类及其子类
			
C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象. Java异常处理通过5个关键字try.ca ...
 - perl学习之子程序
			
一.定义子程序即执行一个特殊任务的一段分离的代码,它可以使减少重复代码且使程序易读.PERL中,子程序可以出现在程序的任何地方.定义方法为:sub subroutine{statements;}二.调 ...
 - ACM训练联盟周赛    Teemo's formula
			
Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...
 - 【20】display,float,position的关系
			
[20]display,float,position的关系 如果display为none,元素不显示. 否则,如果position值为absolute或者fixed,元素绝对定位,float的计算值为 ...