Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D
分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1。假设最后是0的话,那么全部相邻位一定不能全是1,由于假设有一对相邻位全为1,那么这两个的AND值为1。又由于OR值是仅仅要有1。结果就为1。所以这位结果肯定为1。所以就推出了一个dp转移方程。dp[i][j]表示第i位上的数为j时的总个数。那么有:
dp[i][0]=dp[i-1][0]+dp[i-1][1];
dp[i][1]=dp[i-1][0];
设f[i]表示第i位上的总个数,即f[i]=dp[i][0]+dp[i][1].
所以,f[i]=dp[i-1][0]+dp[i-1][1]+dp[i-1][0]
f[i]=f[i-1]+dp[i-1][0]
f[i]=f[i-1]+dp[i-2][0]+dp[i-2][1]
f[i]=f[i-1]+f[i-2]
所以,推到最后可发现这是一个斐波那契!!
所以用矩阵高速幂求结果为0时的情况。然后为1的时候就是2^n-(结果为0的情况值)。
然后由于每一位都是独立的,所以分别推断每一位是0还是1,然后乘起来。
代码例如以下:
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
//const int mod=9901;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=110000+10;
LL mod;
struct Matrix
{
LL ma[3][3];
}init,res;
LL ksm(LL k, LL x)
{
LL ans=1;
while(k){
if(k&1) ans=ans*x%mod;
k>>=1;
x=x*x%mod;
}
return ans;
}
Matrix Mult(Matrix x, Matrix y, int z)
{
Matrix tmp;
for(int i=0; i<z; i++) {
for(int j=0; j<z; j++) {
tmp.ma[i][j]=0;
for(int k=0; k<z; k++) {
tmp.ma[i][j]+=x.ma[i][k]*y.ma[k][j];
if(tmp.ma[i][j]>=mod) tmp.ma[i][j]%=mod;
}
}
}
return tmp;
}
Matrix Pow(Matrix x, LL k, int z)
{
Matrix tmp;
int i, j;
for(i=0; i<z; i++) for(j=0; j<z; j++) tmp.ma[i][j]=(i==j);
while(k) {
if(k&1) tmp=Mult(tmp,x,z);
x=Mult(x,x,z);
k>>=1;
}
return tmp;
}
int main()
{
LL n, k, l, x1, x2, ans, tmp, i;
while(scanf("%I64d%I64d%I64d%I64d",&n,&k,&l,&mod)!=EOF){
if(l<=62&&k>=((LL)1<<l)){
puts("0");
continue ;
}
init.ma[0][0]=init.ma[0][1]=1;
init.ma[1][0]=1;
init.ma[1][1]=0;
res=Pow(init,n-2,2);
tmp=ksm(n,(LL)2);
x1=(res.ma[0][1]*2%mod+res.ma[0][0]*3%mod)%mod;
x2=(tmp+mod-x1)%mod;
ans=1;
for(i=0;i<l;i++){
if(k&((LL)1<<i))
ans=ans*x2%mod;
else ans=ans*x1%mod;
}
printf("%I64d\n",ans%mod);
}
return 0;
}
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
得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...
- 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest
题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...
- Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)
Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...
- 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 ...
随机推荐
- 2.flume架构以及核心组件
flume组件主要包含三部分 source:从各个地方收集数据 channel:聚集,相当于临时数据存放的地方.因为数据来的时候,不可能来一条便写一次,那样效率太低,而是先把数据放在通道里,等通道满了 ...
- request模拟知乎登录(无验证码机制)
import request try: import cookielib #python2版本 except: import http.cookiejar as cookielib #python3版 ...
- 记录一次lnmp故障报告
业务架构图: nginx 状态监控图: 本次故障的表现为:前端php页面无法打开,空白页或者502错误. nginx中php配置如下: location ~ \.php$ { root /xxx/xx ...
- tarjan算法和Kosaraju算法
tarjan算法和Kosaraju算法是求有向图的强连通分量的算法: #include<iostream> #include<cstring> using namespace ...
- 杀掉TOMCAT并重启的脚本
/usr/local/tomcat7/bin/shutdown.sh sleep #具体时间就看你得webapp在调用shutdown.sh后多久后处于僵死状态 ps -ef | grep sleep ...
- 网络大数据分析 -- 使用 ElasticSearch + LogStash + Kibana 来可视化网络流量
https://blog.csdn.net/yeasy/article/details/45332493
- (十二)MySQL逻辑备份mysqldump
(1)简介 语法 mysqldump -h服务器 -u用户名 -p密码 [-P端口号] [参数] 数据库名 >备份文件.sql 关于数据库: -A,--all-databases 所有库,会生成 ...
- Kattis - boxes (dfn序)
Boxes There are N boxes, indexed by a number from 1 to N . Each box may (or not may not) be put into ...
- 15、Django实战第15天:我要学习咨询
今天完成的是课程机构列表页面的最后一个模块:我要学习 我们在models中创建对应的表时UserAsk.之前我们讲过:在做表单的时候,我们可以通过forms先对提交的数据做验证,之前我们使用的是For ...
- [P2396] yyy loves Maths VII
Link: P2396 传送门 Solution: 一眼能看出$O(n*2^n)$的状压$dp$ 但此题是个卡常题,$n=23/24$的时候就别想过了 这题算是提供了一种对状压$dp$的优化思路吧 原 ...