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 ...
随机推荐
- Zookeeper与Eureka的区别
Zookeeper与Eureka的区别 想要了解Zk与eureka的区别首先要知道CAP定理 CAP定理 Mysql强一致性(数据唯一出处),设计数据库设计的三范式 (表必须有主键:表不能有重复的列: ...
- C do whlie 数数位
#include <stdio.h> int main(int argc, char **argv) { //定义两个变量 x 跟 n,n的初始化为0: int x; int n ...
- 【halcon】算子
算子 rgb1_to_gray 灰度化 threshold:英文是阈的意思 二值化算子 Connection Compute connected components of a region. ...
- 饥饿的小易(枚举+广度优先遍历(BFS))
题目描述 小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃.最开始小易在一个初始位置x_0.对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7.因为使 ...
- phantomjs抛出IOException
使用phantomjs对网页进行截图遇到的问题 问题描述: 使用的phantomjs版本:phantomjs-2.1.1-windows 使用的截图js文件,\phantomjs-2.1.1-wind ...
- Executor Framework
Why? look at the following 2 pieces of code for implementing a simple web server based on socket, ca ...
- opencv-学习笔记(4)-模糊
opencv-学习笔记(4)-模糊 本章要点: 4种模糊方式 2d卷积 Cv2.filter2D(‘图像对象’,‘目标图像这里直接设为-1即可’,kernal,anchor(-1,-1)) 一般后一个 ...
- HDU 2491 Priest John's Busiest Day(贪心)(2008 Asia Regional Beijing)
Description John is the only priest in his town. October 26th is the John's busiest day in a year be ...
- 将footer固定在页面最下方
方法一: HTML结构: <div id="id_wrapper"> <div id="id_header"> Header Block ...
- 20172330 2017-2018-1 《Java程序设计》第九周学习总结
20172330 2017-2018-1 <程序设计与数据结构>第九周学习总结 教材学习内容总结 本周的学习包括两章内容,分别为异常和递归. 异常 错误和异常都是对象,代表非正常情况或者无 ...