题目地址: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. 【 Python 】函数的参数

    一.默认参数: 默认参数可以简化函数的调用,设置默认参数时,有几点要注意: 1,必选参数在前,默认参数在后,否则python的解释器会报错. 2,如何设置默认参数. 当函数有多个参数时,把变化大的参数 ...

  2. vmware的3种网络模式

    ####图片以及部分内容来源:https://note.youdao.com/share/?id=236896997b6ffbaa8e0d92eacd13abbf&type=note#/ 在安 ...

  3. sql参数化防止sql注入导致的暴露数据库问题

    #转载请联系 假如你在京东工作,你要做的任务就是做一个商品搜索的东西供用户使用. 然后你写出了这么一个程序的雏形. import pymysql def main(): conn = pymysql. ...

  4. HDU-3221

    Brute-force Algorithm Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. hdu 5170(数学)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. React Native - 1 Windows下的环境配置(Windows+Android)

    参考:https://facebook.github.io/react-native/docs/getting-started.html(要FQ)     网站上建议使用Chocolatey去配环境, ...

  7. PHP的Trait

    PHP的Trait Trait是在PHP5.4中加入的,它既不是接口也不是类.主要是为了解决单继承语言的限制.是PHP多重继承的一种解决方案.例如,需要同时继承两个 Abstract Class, 这 ...

  8. linux:/lib/libc.so.6: version `glibc_2.7′ not found【没有解决】采用新方法达到目的

    1 下载glibc wget http://ftp.gnu.org/pub/gnu/glibc/glibc-2.7.tar.gz 2. tar zxf glibc-2.7.tar.gz 3. cd g ...

  9. Java中应用多线程的场景?

    最典型的应用比如tomcat,tomcat内部采用的就是多线程,上百个客户端访问同一个web应用,tomcat接入后都是把后续的处理扔给一个新的线程来处理,这个新的线程最后调用到我们的servlet程 ...

  10. 设计高效SQL: 一种视觉的方法

    行; 这听起来很直观,但最有效的方法是什么?你可能有如下选择:行,其中有50行你必须剔除行,其中有450行你必须剔除行中剔除50行听起来比从500行中剔除450行更高效,但是请记住:聚簇,或者说,数据 ...