题目地址: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. net core服务器缺包,如何在线安装?

    Install -package命令不行. 下面命令也不行.求助大家,怎么安装?

  2. KDJ回测

    # -*- coding: utf-8 -*- import os import pandas as pd # ========== 遍历数据文件夹中所有股票文件的文件名,得到股票代码列表stock_ ...

  3. GO语言的数据结构测试

    用于docker了,go也慢慢看一些.. 推荐书籍<go语言实践>就是<Go in Action>的中文版,有文字版PDF的. package main import ( &q ...

  4. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  5. OfficeAddin基础

    运行的机器制

  6. Codeforces #439 Div2 E

    #439 Div2 E 题意 给出二维平面,有多个询问: 把某一区域围起来(围墙之间无交点) 移除某一区域的围墙(此时保证围墙一定存在) 选定两个位置问是否可以互相到达 分析 看起来很复杂,其实这道题 ...

  7. Codeforces 1059E. Split the Tree

    题目:http://codeforces.com/problemset/problem/1059/E 用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个 ...

  8. Web应用程序指纹识别工具BlindElephant

    Web应用程序指纹识别工具BlindElephant   BlindElephant是一款Web应用程序指纹识别工具.该工具可以读取目标网站的特定静态文件,计算其对应的哈希值,然后和预先计算出的哈希值 ...

  9. 【费用流】BZOJ1061[NOI2008]-志愿者招募

    [题目大意] 一个项目需要n天完成,其中第i天至少需要Ai个人.共有m类人可以招募,其中第i类可以从第Si天做到第Ti天,每人的招募费用为Ci元.求最小招募费用. [思路] byvoid神犇的建图详解 ...

  10. python3 开发面试题(创建表结构)6.9

    纯sql语句写出: '''设计 图书管理系统 表结构: - 书 - 书名 - 作者 - 姓名 - 出版社 - 出版社名称 - 地址 一本书只能由一家出版社出版 --> 多对一(书对出版社) 一本 ...