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区间内,对 ...
随机推荐
- CPP-基础:内部函数应该在当前源文件中说明和定义
static函数与普通函数作用域不同,仅在本文件.只在当前源文件中使用的函数应该说明为内部函数(static),内部函数应该在当前源文件中说明和定义.对于可在当前源文件以外使用的函数,应该在一个头文件 ...
- PHP 递归无限极下级
下面是自己用到的一些递归方法,当然都是借鉴的,各位看官请勿怪 第一种 有层级 $array = array( array('id' => 1, 'pid' => 0, 'n' => ...
- shell脚本,awk实现每个数字加1.
[root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...
- javaEE(12)_数据库连接池
一.直接获取数据库连接和通过池获取示意图: 二.编写数据库连接池 1.实现DataSource接口,并实现连接池功能的步骤: •在DataSource构造函数中批量创建与数据库的连接,并把创建的连接加 ...
- vue建项目并使用
今天来回顾下vue项目的建立和使用,好久不用感觉不会用了. 下面两个都要全局安装 首先安装git,地址 https://gitforwindows.org/ 安装node, 地址 https://n ...
- PAT 乙级 1011
题目 题目地址:PAT 乙级 1011 思路 这道题的比较坑的地方在于给定数据的范围 int 类型的数据大小是[-2^31 , 2^31 -1] 即 [-2147483648,2147483647] ...
- TB平台搭建之二
主要想记录关于debug问题: 一般我会1.定位问题所在位置比如使能信号错误.地址读写错误.数据流pipeline错误.... 2.首先看问题的源头(对应信号)是否还正确,比如出现XX要查看她的第一级 ...
- [php] 表单注意事项
什么是 htmlspecialchars()方法? htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 预定义的字符是: & (和号) 成为 & &q ...
- java 比较String StringBuffer StringBuilder
String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...
- Python9-面对对象2-day23
#计算正方形的周长和面积 class Square: def __init__(self,side_len): self.side_len = side_len def perimeter(self) ...