uva1386(矩阵快速幂)
2015-07-31 16:35
316 查看
题意:
一个细胞自动机,有n个格子,每个格子的取值为0~m-1。给定距离d,每次操作后每个格子的值将变为到它距离不超过d的所有格子在操作之前的值之和除以m的余数。
思路:
矩阵快速幂
一个细胞自动机,有n个格子,每个格子的取值为0~m-1。给定距离d,每次操作后每个格子的值将变为到它距离不超过d的所有格子在操作之前的值之和除以m的余数。
思路:
矩阵快速幂
#include <cstdio> #include <cstring> #define ll long long const int N = 505; int n, M, D, K; struct Mat { ll arr ; }; Mat ceil, x; Mat mul (const Mat& a, const Mat& b) { Mat ans; memset(ans.arr, 0, sizeof(ans.arr)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) ans.arr[i] = (ans.arr[i] + a.arr[j] * b.arr[(i-j+n)%n]) % M; } return ans; } void Pow (int n) { Mat ans = x; while (n) { if (n&1) ans = mul(ans, x); x = mul(x, x); n >>= 1; } x = ans; } int main () { while (scanf("%d%d%d%d", &n, &M, &D, &K) == 4) { for (int i = 0; i < n; i++) scanf("%lld", &ceil.arr[i]); memset(x.arr, 0, sizeof(x.arr)); for (int i = -D; i <= D; i++) x.arr[(i+n)%n] = 1; Pow(K-1); for (int i = 0; i < n; i++) { if (i) printf(" "); ll ret = 0; for (int j = 0; j < n; j++) ret = (ret + ceil.arr[j] * x.arr[(j-i+n)%n]) % M; printf("%lld", ret); } printf("\n"); } return 0; }
相关文章推荐
- 一个正则表达式验证密码强度
- Spring依赖注入——java项目中使用spring注解方式进行注入
- HDU 4751 — Divide Groups 交叉染色判二分图
- Magnum的使用
- iOS学习之KVO、KVC
- 【Android应用开发技术:媒体开发】MediaPlayer基本原理
- C++的三种继承方式
- 赛门铁克ssl证书 仲裁证书
- php变量的引用计数器和写时复制
- FFT 快速傅里叶变换 hdu1402 hdu4609
- 大端和小端的区别和方便记忆的方法
- POJ - 2484 A Funny Game
- 8数码问题的C++解决方案
- Android LaunchMode and StartActivityForResult
- Ehcache介绍
- 多播(组播)与广播的技术实现
- 20150726 Web前端开发基础html+css
- dwr js与java互相调用
- php函数注释
- SylixOS动态加载器系列文章(5) 应用程序加载原理