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区间内,对 ...
随机推荐
- 关于js作用域问题详解
执行上下文 函数表达式和函数声明 1. console.log(a); // ReferenceError: a is not defined // ReferenceError(引用错误)对象表明一 ...
- 换个语言学一下 Golang (1)
做技术的总是有些拗.这么多年一直在.net的框框里打转转.直到现在市场上.net工作越来越难找,项目越来越老才发现不做出改变不行了.就从学习Go开始吧. Go语言的特点 简洁.快速.安全 并行.有趣. ...
- NOIP 2018数据点
链接: https://pan.baidu.com/s/14jXQGPSac3b9_m5h5x2wGQ 提取码: 1cbk 好文别忘点赞!!!
- (39)zabbix snmp自定义OID nginx监控实例
为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...
- (26)zabbix脚本报警介质自定义(钉钉)
zabbix机器人告警配置 首先在钉钉中创建一个群然后设置群机器人添加自定义机器人(webhook...) 添加后复制其中的webhook地址到报警脚本dingding.py中的webhook=... ...
- Xshell 配色方案 Ubuntu Solarized_Dark isayme
前言 最近在用Ubuntu,发现它的配色方案挺好看的,所以查了下有没有大神做过Xshell的Ubuntu配色方案. 一看,果然还是有大佬做了这个的. 三套配色配置如下: 1. Ubuntu的Solar ...
- usb3.0驱动
usb3.0驱动下载地址 华硕注入usb3.0驱动工具下载地址 https://dlsvr04.asus.com/pub/ASUS/misc/utils/ASUS_EZInstaller_V10306 ...
- iOS使用Reveal分析他人app界面
本文转自http://blog.csdn.net/cuibo1123/article/details/45694657 安装: 首先前往 http://revealapp.com/download/ ...
- SQLAlchemy常用操作
Models 只是配置和使用比较简单,因为他是Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy 真正算得上全面的ORM框架必然是我们的SQLAlch ...
- 00051_static关键字
1.static概念 当在定义类的时候,类中都会有相应的属性和方法.而属性和方法都是通过创建本类对象调用的.当在调用对象的某个方法时,这个方法没有访问到对象的特有数据时,方法创建这个对象有些多余.可是 ...