题目地址: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. 多个springboot项目部署在同一tomcat上,出现jmx错误

    多个springboot项目部署在同一tomcat上,出现jmx错误 原因:因为jmx某些东西重复,禁用jmx就可以了 endpoints.jmx.unique-names=true

  2. Android的简单应用(三)——为你的程序添加监听器

    平时在写程序时经常会遇到监听器,比如按钮的click监听器,按键监听器等等.而android中的监听器和java中的回调函数是同一个概念,都是在底层代码中定义一个接口来调用高层的代码.那么什么是回调函 ...

  3. 使用AutoMapper 处理DTO数据对象的转换

    using AutoMapper;using System; namespace DTOtEST{ class Program { static void Main(string[] args) { ...

  4. Linux下如何批量转码iconv

    来源:http://hi.baidu.com/curioz/blog/item/2555863514f9491d90ef390d.html 下载了不少文本txt,如verycd上的致纯书苑,解压看看是 ...

  5. python的垃圾回收机制和析构函数__del__

    析构函数__del__定义:在类里定义,如果不定义,Python 会在后台提供默认析构函数. 析构函数__del__调用: A.使用del 显式的调用析构函数删除对象时:del对象名: class F ...

  6. saturate_cast防越界函数

    CV_IMAGE_ELEM(img2,uchar,i,j*3+c)=saturate_cast<uchar>(alpha*( CV_IMAGE_ELEM(img,uchar,i,j*3+c ...

  7. hadoop学习一:hadoop安装(hadoop2.4.1,ubuntu14.04)

    1.创建用户 adduser hduser 修改hduser用户权限: sudo vim /ect/sudoers ,在文件中增加 hduser  ALL=(ALL:ALL) ALL .

  8. MySql笔记之数据表

    数据表:行称为记录  列称为字段 用来存储数据 一.数据类型 数据类型是指列.存储过程参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. 在我们存储不同类型的数据时,为了 ...

  9. requests库使用socks5代理

    备查: #!usr/bin/env python # coding=utf-8 import requests proxies = {'https': 'https://127.0.0.1:1080' ...

  10. Tarjan求LCA总结

    Tarjan算法向上标记法:从x向上走到根节点,并标记所有经过的点从y向上走到根节点,当第一次遇到已标记的节点时,就找到了LCA(x, y)对于每个询问,向上标记法的时间复杂度最坏为O(n) 在深度遍 ...