题目地址: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 (矩阵高速幂)的更多相关文章

  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

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

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

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

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

  5. Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)

    Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...

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

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

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

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

随机推荐

  1. 解决:centos7.3 tomcat7启动巨慢问题

    目前公司大部分服务器操作系统还是centos6.5,tomcat用的是7,平时基本上没什么问题,启动也比较快,但是,最近有部分项目服务器更新至centos7.3 ,有些机器启动tomcat的时候巨慢无 ...

  2. jQuery 入门笔记1

    jQuery是一个兼容多浏览器的javascript框架,核心理念是write less,do more(写得更少,做得更多). 1:jQuery使用 <script type="te ...

  3. 区块链开发(五)git、truffle安装

    truffle是以太坊最受欢迎的一个开发框架,本篇博客介绍truffle的下载安装过程. git安装 在安装truffle之前需要核实一下本机是否安装git程序.后面的程序安装需要依赖git. 输入以 ...

  4. Selenium2+python自动化55-unittest之装饰器(@classmethod)【转载】

    前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...

  5. 第三篇:使用firewall-cmd

    本文来源:Working with firewalld         其它参考文档:redhat官方中文文档 1.查询类命令 2.设置类命令 3.运行时zone设置 4.永久性zone设置 5.直接 ...

  6. phython正则表达式 Python Re模块

    反斜杠问题 与大多数编程语言相同,正则表达式里使用”\”作为转义字符,这就可能造成反斜杠困扰.假如你需要匹配文本中的字符”\”, Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式 ...

  7. UVALive 3882.And Then There Was One-约瑟夫问题(递推)

    And Then There Was One Time limit: 3.000 seconds Let’s play a stone removing game. Initially, n ston ...

  8. SSH框架的简单含义

    典型的J2EE三层结构,分为表现层.中间层(业务逻辑层)和数据服务层.三层体系将业务规则.数据访问及合法性校验等工作放在中间层处理.客户端不直接与数据库交互,而是通过组件与中间层建立连接,再由中间层与 ...

  9. [BZOJ 1228] E&D

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1228 Solution: 感觉自己对博弈论的理论一直了解得不够透彻 一篇讲原理的文章:S ...

  10. 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows

    题目描述 农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏.他建造的围栏必须包括他的奶牛喜欢吃草的所有地点.对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度. 输入输出格式 输入 ...