CodeForces 551D GukiZ and Binary Operations DP+矩阵乘法
来源:互联网 发布:手机淘宝查看退货率 编辑:程序博客网 时间:2024/04/30 01:53
dp方程和fib数列一样。。
#include <cstdio>#include <cstring>#define rep(i,j,k) for(int i=j;i<k;i++)typedef long long ll;ll n, K, l, mod;struct Matrix { ll v[2][2]; Matrix(ll x = 0) { rep(i,0,2)rep(j,0,2)v[i][j]=0;rep(i,0,2) v[i][i] = x; } ll *operator [](int x) { return v[x]; } friend Matrix operator *(Matrix a, Matrix b){ Matrix c; rep(i,0,2) rep(j,0,2) rep(k,0,2) c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod; return c; } friend Matrix operator ^(Matrix a, ll n){ Matrix ans(1); for (; n; n >>= 1, a = a * a) if (n & 1) ans = ans * a; return ans; }} b;ll quick_pow(ll a, ll n) { ll ans = 1; for (; n; n /= 2, a = a * a % mod) if (n & 1) ans = ans * a % mod; return ans;}int main() { b[0][0]=b[0][1]=b[1][0]=1; scanf("%I64d%I64d%I64d%I64d", &n, &K, &l, &mod); if (l < 63 && 1ll << l <= K || mod == 1) { puts("0"); return 0; } ll ans = 1, fib = (b ^ (n + 1))[0][0], pow2 = quick_pow(2, n), t = (pow2 - fib + mod) % mod; for (int i = 0; i < l; i++) if (((K >> i) & 1) == 0) ans = ans * fib % mod; else ans = ans * t % mod; printf("%I64d", ans); return 0;}
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 , inC/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}
- CodeForces 551D GukiZ and Binary Operations DP+矩阵乘法
- CodeForces 551 D.GukiZ and Binary Operations(dp+矩阵快速幂)
- Codeforces 551D GukiZ and Binary Operations
- Codeforces 551 D. GukiZ and Binary Operations
- codeforces 551 D. GukiZ and Binary Operations
- Codeforces Round #307 (Div. 2) 551D - GukiZ and Binary Operations 矩阵快速幂
- CF 551D GukiZ and Binary Operations
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵快速幂)
- cf#307-D. GukiZ and Binary Operations-矩阵快速幂
- Codeforces Round #307 (Div. 2)--C. GukiZ hates Boxes、D. GukiZ and Binary Operations
- GukiZ and Binary Operations(矩阵+二进制)
- Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations(数学)
- codeforces #307 D. GukiZ and Binary Operations(各种快速幂+斐波那契)
- CF551D:GukiZ and Binary Operations(矩阵 & 思维)
- Codeforces D. Professor GukiZ and Two Arrays
- 【矩阵乘法优化DP】Codeforces 717D Dexterina’s Lab
- CF 551DGukiZ and Binary Operations 矩阵/位运算
- Codeforces 551E GukiZ and GukiZiana (分块)
- android studio使用错误排查记录
- Liunx学习笔记(5)之解压缩
- 在cmd窗口运行需要加载含驱动的java程序
- linux下shell命令之hwclock
- 算法学习--查找(一)
- CodeForces 551D GukiZ and Binary Operations DP+矩阵乘法
- Can't open the mysql.plugin table. Please run mysql_upgrade to create it
- Unity 行为树 Behavior Designer
- hibernate.hbm2ddl.auto含义介绍
- C语言基础知识1
- Java NIO客户端和服务端(聊天室:单聊,群聊,服务端广播消息,上线以及上线人数通知,下线通知)
- 缓冲加载图片的 jQuery 插件 lazyload.js 使用方法详解
- Linux学习笔记(6)之vi编辑器基本操作
- Linux下Rsync+sersync实现数据双向实时同步