Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂
1 second
256 megabytes
standard input
standard output
We all know that GukiZ often plays with arrays.
Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: ? Here operation
means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation
means bitwise OR (in Pascal it is equivalent to
, in C/C++/Java/Python it is equivalent to |).
Because the answer can be quite large, calculate it modulo m. This time GukiZ hasn't come up with solution, and needs you to help him!
First and the only line of input contains four integers n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ m ≤ 109 + 7).
In the single line print the number of arrays satisfying the condition above modulo m.
2 1 2 10
3
2 1 1 3
1
3 3 2 10
9
In the first sample, satisfying arrays are {1, 1}, {3, 1}, {1, 3}.
In the second sample, only satisfying array is {1, 1}.
In the third sample, satisfying arrays are {0, 3, 3}, {1, 3, 2}, {1, 3, 3}, {2, 3, 1}, {2, 3, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}.
题意:n个小于(2^l)的数执行要求的运算等于K的方案数%m的结果
题解:from jhz033
思路:首先看到或,并就想将这个数拆开为二进制的01串,分别考虑每一位的0,1;
当前k的那个位置为0时,表示a1-an中没有两个相邻的1;
同理,当前k为为1时,表示a1-an中有两个相邻的1;2^n,减去0的方案即是;
刚刚开始一直在想组合数学的求法,发现不好写(。。。我也不会)
后来发现dp可以做,但是n很大;
dp方程:dp[i][0]=dp[i-1][1]+dp[i-1][0];
dp[i][1]=dp[i-1][0];
dp[i][j]表示第i位为j的无相邻1的方案数;
乍一看很像斐波那契,构造矩阵;
[ 1 , 1 ]
[ dp[i-1][0] , dp[i-1][1] ] *[ 1 , 0 ] =[ dp[i][0] , dp[i][1] ];
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll n,k,l,m;
struct matrix
{
ll m[][];
} ans,exm; struct matrix matrix_mulit(struct matrix aa,struct matrix bb)
{
struct matrix there;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
there.m[i][j]=;
for(int k=;k<;k++)
there.m[i][j]=(there.m[i][j]+aa.m[i][k]*bb.m[k][j]%m)%m;
}
}
return there;
}
ll matrix_quick(ll gg)
{
exm.m[][]=exm.m[][]=exm.m[][]=;
exm.m[][]=;
ans.m[][]=;ans.m[][]=;
ans.m[][]=;ans.m[][]=;
if(gg==)
return ;
while(gg)
{
if(gg&)
{
ans=matrix_mulit(ans,exm);
}
exm = matrix_mulit(exm, exm);
gg >>= ;
}
return (ans.m[][]+ans.m[][])%m;
}
ll quick(ll aa,ll bb)
{
ll re=;
while(bb)
{
if(bb&)
{
re=(re*aa)%m;
}
aa=(aa*aa)%m;
bb>>=;
}
return re;
}
int main()
{
scanf("%I64d %I64d %I64d %I64d",&n,&k,&l,&m);
ll ling=,yi=;
int flag=;
while(k)
{
if(k%==)
ling++;
else
yi++;
if(ling+yi>l&&k%==)
{
flag=;
}
k>>=;
}
if(flag)
{
printf("0\n");
return ;
}
ll lingmod=,yimod=;
lingmod=matrix_quick(n-);
yimod=((quick(,n)-lingmod)%m+m)%m;
ling+=l-(yi+ling);
printf("%I64d\n",quick(lingmod,ling)*quick(yimod,yi)%m);
return ;
}
Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂的更多相关文章
- 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ
题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...
- 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest
题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...
- 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 #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)
题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...
- Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)
传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...
- 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) B. ZgukistringZ 暴力
B. ZgukistringZ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/probl ...
随机推荐
- SQL语言重点学习
数据库的操作任务通常包括以下几个方面: 1.查询数据. 2.在表中插入,修改和删除记录. 3.建立,修改和删除数据对象. 4.控制对数据和数据对象的读写. 5.保证数据库一致性和完整性. SQL语言学 ...
- 【cover-view、cover-image】 覆盖组件说明
cover-view.cover-image 这两类覆盖组件用于显示在一些特殊组件上方(map.video.canvas.camera.live-player.live-pusher). 这类组件一般 ...
- [问题] docker: Failed to start Docker Application Container Engine.
docker无法启动: # systemctl restart docker Job for docker.service failed because the control process exi ...
- JS验证验证服务器控件
JS验证验证服务器控件 <script language="javascript" type="text/javascript"> /******* ...
- Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again YUM报错
1.挂盘 ----- 2.# mount /dev/sr0 /media/ mount: block device /dev/sr0 is write-protected, mounting ...
- Reversing Encryption(模拟水题)
A string ss of length nn can be encrypted(加密) by the following algorithm: iterate(迭代) over all divis ...
- Java学习个人备忘录之数组工具类
下面主要讲解一个针对数组操作的工具类. a.java -- 工具类文件 //按理来说要先编译本文件, 然后再编译主函数 class ArrayTool { /* 获取整型数组的最大值 */ publi ...
- oracle数据库中常见的操作语句(一)
一 创建表空间 create tablespace lfdc_data logging datafile 'D:\Database\lfdc_data.dbf' size 50m autoextend ...
- C++视频教学
http://open.163.com/movie/2010/1/E/4/M6LDTAPTU_M6LFSTGE4.html http://open.163.com/movie/2010/1/N/5/M ...
- BAT批处理(五)
批处理程序 一.交互界面设计 没啥说的,看看设计的菜单界面吧:@echo offclstitle 终极多功能修复:menuclscolor 0Aecho.echo ================== ...