题意概述:

等价地,本题可以转化为下面的问题:

考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$。求$A^k$。

数据范围:

$n \leq 500, k \leq 10000000, d < \frac{n}{2} $。

分析:

很容易想到矩阵快速幂$O(n^3log(k))$的解法,但是很可惜,矩阵有点大,用通用方法难免超时。尝试计算矩阵较小的幂,发现得到的矩阵的每一行

都可由上一行循环右移$1$位得到。因此只计算一行就以为计算出整个矩阵,因此复杂度降为$O(n^2log(k))$,可以通过。

  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <ctime>
  9. #include <cmath>
  10. #include <iostream>
  11. #include <assert.h>
  12. #define PI acos(-1.)
  13. #pragma comment(linker, "/STACK:102400000,102400000")
  14. #define max(a, b) ((a) > (b) ? (a) : (b))
  15. #define min(a, b) ((a) < (b) ? (a) : (b))
  16. #define mp make_pair
  17. #define st first
  18. #define nd second
  19. #define keyn (root->ch[1]->ch[0])
  20. #define lson (u << 1)
  21. #define rson (u << 1 | 1)
  22. #define pii pair<int, int>
  23. #define pll pair<ll, ll>
  24. #define pb push_back
  25. #define type(x) __typeof(x.begin())
  26. #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
  27. #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
  28. #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
  29. #define dbg(x) cout << x << endl
  30. #define dbg2(x, y) cout << x << " " << y << endl
  31. #define clr(x, i) memset(x, (i), sizeof(x))
  32. #define maximize(x, y) x = max((x), (y))
  33. #define minimize(x, y) x = min((x), (y))
  34. #define low_bit(x) ((x) & (-x))
  35. using namespace std;
  36. typedef long long ll;
  37. const int int_inf = 0x3f3f3f3f;
  38. const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
  39. const int INT_INF = (int)((1ll << ) - );
  40. const double double_inf = 1e30;
  41. const double eps = 1e-;
  42. typedef unsigned long long ul;
  43. inline int readint(){
  44. int x;
  45. scanf("%d", &x);
  46. return x;
  47. }
  48. inline int readstr(char *s){
  49. scanf("%s", s);
  50. return strlen(s);
  51. }
  52. //Here goes 2d geometry templates
  53. struct Point{
  54. double x, y;
  55. Point(double x = , double y = ) : x(x), y(y) {}
  56. };
  57. typedef Point Vector;
  58. Vector operator + (Vector A, Vector B){
  59. return Vector(A.x + B.x, A.y + B.y);
  60. }
  61. Vector operator - (Point A, Point B){
  62. return Vector(A.x - B.x, A.y - B.y);
  63. }
  64. Vector operator * (Vector A, double p){
  65. return Vector(A.x * p, A.y * p);
  66. }
  67. Vector operator / (Vector A, double p){
  68. return Vector(A.x / p, A.y / p);
  69. }
  70. bool operator < (const Point& a, const Point& b){
  71. return a.x < b.x || (a.x == b.x && a.y < b.y);
  72. }
  73. int dcmp(double x){
  74. if(abs(x) < eps) return ;
  75. return x < ? - : ;
  76. }
  77. bool operator == (const Point& a, const Point& b){
  78. return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
  79. }
  80. double Dot(Vector A, Vector B){
  81. return A.x * B.x + A.y * B.y;
  82. }
  83. double Len(Vector A){
  84. return sqrt(Dot(A, A));
  85. }
  86. double Angle(Vector A, Vector B){
  87. return acos(Dot(A, B) / Len(A) / Len(B));
  88. }
  89. double Cross(Vector A, Vector B){
  90. return A.x * B.y - A.y * B.x;
  91. }
  92. double Area2(Point A, Point B, Point C){
  93. return Cross(B - A, C - A);
  94. }
  95. Vector Rotate(Vector A, double rad){
  96. //rotate counterclockwise
  97. return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
  98. }
  99. Vector Normal(Vector A){
  100. double L = Len(A);
  101. return Vector(-A.y / L, A.x / L);
  102. }
  103. void Normallize(Vector &A){
  104. double L = Len(A);
  105. A.x /= L, A.y /= L;
  106. }
  107. Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
  108. Vector u = P - Q;
  109. double t = Cross(w, u) / Cross(v, w);
  110. return P + v * t;
  111. }
  112. double DistanceToLine(Point P, Point A, Point B){
  113. Vector v1 = B - A, v2 = P - A;
  114. return abs(Cross(v1, v2)) / Len(v1);
  115. }
  116. double DistanceToSegment(Point P, Point A, Point B){
  117. if(A == B) return Len(P - A);
  118. Vector v1 = B - A, v2 = P - A, v3 = P - B;
  119. if(dcmp(Dot(v1, v2)) < ) return Len(v2);
  120. else if(dcmp(Dot(v1, v3)) > ) return Len(v3);
  121. else return abs(Cross(v1, v2)) / Len(v1);
  122. }
  123. Point GetLineProjection(Point P, Point A, Point B){
  124. Vector v = B - A;
  125. return A + v * (Dot(v, P - A) / Dot(v, v));
  126. }
  127. bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
  128. //Line1:(a1, a2) Line2:(b1,b2)
  129. double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
  130. c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
  131. return dcmp(c1) * dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
  132. }
  133. bool OnSegment(Point p, Point a1, Point a2){
  134. return dcmp(Cross(a1 - p, a2 - p)) == && dcmp(Dot(a1 - p, a2 -p)) < ;
  135. }
  136. Vector GetBisector(Vector v, Vector w){
  137. Normallize(v), Normallize(w);
  138. return Vector((v.x + w.x) / , (v.y + w.y) / );
  139. }
  140.  
  141. bool OnLine(Point p, Point a1, Point a2){
  142. Vector v1 = p - a1, v2 = a2 - a1;
  143. double tem = Cross(v1, v2);
  144. return dcmp(tem) == ;
  145. }
  146. struct Line{
  147. Point p;
  148. Vector v;
  149. Point point(double t){
  150. return Point(p.x + t * v.x, p.y + t * v.y);
  151. }
  152. Line(Point p, Vector v) : p(p), v(v) {}
  153. };
  154. struct Circle{
  155. Point c;
  156. double r;
  157. Circle(Point c, double r) : c(c), r(r) {}
  158. Circle(int x, int y, int _r){
  159. c = Point(x, y);
  160. r = _r;
  161. }
  162. Point point(double a){
  163. return Point(c.x + cos(a) * r, c.y + sin(a) * r);
  164. }
  165. };
  166. int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
  167. double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
  168. double e = a * a + c * c, f = * (a * b + c * d), g = b * b + d * d - C.r * C.r;
  169. double delta = f * f - * e * g;
  170. if(dcmp(delta) < ) return ;
  171. if(dcmp(delta) == ){
  172. t1 = t2 = -f / ( * e); sol.pb(L.point(t1));
  173. return ;
  174. }
  175. t1 = (-f - sqrt(delta)) / ( * e); sol.pb(L.point(t1));
  176. t2 = (-f + sqrt(delta)) / ( * e); sol.pb(L.point(t2));
  177. return ;
  178. }
  179. double angle(Vector v){
  180. return atan2(v.y, v.x);
  181. //(-pi, pi]
  182. }
  183. int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
  184. double d = Len(C1.c - C2.c);
  185. if(dcmp(d) == ){
  186. if(dcmp(C1.r - C2.r) == ) return -; //two circle duplicates
  187. return ; //two circles share identical center
  188. }
  189. if(dcmp(C1.r + C2.r - d) < ) return ; //too close
  190. if(dcmp(abs(C1.r - C2.r) - d) > ) return ; //too far away
  191. double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
  192. double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / ( * C1.r * d));
  193. Point p1 = C1.point(a - da), p2 = C1.point(a + da);
  194. sol.pb(p1);
  195. if(p1 == p2) return ;
  196. sol.pb(p2);
  197. return ;
  198. }
  199. int GetPointCircleTangents(Point p, Circle C, Vector* v){
  200. Vector u = C.c - p;
  201. double dist = Len(u);
  202. if(dist < C.r) return ;//p is inside the circle, no tangents
  203. else if(dcmp(dist - C.r) == ){
  204. // p is on the circles, one tangent only
  205. v[] = Rotate(u, PI / );
  206. return ;
  207. }else{
  208. double ang = asin(C.r / dist);
  209. v[] = Rotate(u, -ang);
  210. v[] = Rotate(u, +ang);
  211. return ;
  212. }
  213. }
  214. int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
  215. //a[i] store point of tangency on Circle A of tangent i
  216. //b[i] store point of tangency on Circle B of tangent i
  217. //six conditions is in consideration
  218. int cnt = ;
  219. if(A.r < B.r) { swap(A, B); swap(a, b); }
  220. int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y);
  221. int rdiff = A.r - B.r;
  222. int rsum = A.r + B.r;
  223. if(d2 < rdiff * rdiff) return ; // one circle is inside the other
  224. double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
  225. if(d2 == && A.r == B.r) return -; // two circle duplicates
  226. if(d2 == rdiff * rdiff){ // internal tangency
  227. a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
  228. return ;
  229. }
  230. double ang = acos((A.r - B.r) / sqrt(d2));
  231. a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
  232. a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
  233. if(d2 == rsum * rsum){
  234. //one internal tangent
  235. a[cnt] = A.point(base);
  236. b[cnt++] = B.point(base + PI);
  237. }else if(d2 > rsum * rsum){
  238. //two internal tangents
  239. double ang = acos((A.r + B.r) / sqrt(d2));
  240. a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
  241. a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
  242. }
  243. return cnt;
  244. }
  245. Point ReadPoint(){
  246. double x, y;
  247. scanf("%lf%lf", &x, &y);
  248. return Point(x, y);
  249. }
  250. Circle ReadCircle(){
  251. double x, y, r;
  252. scanf("%lf%lf%lf", &x, &y, &r);
  253. return Circle(x, y, r);
  254. }
  255. //Here goes 3d geometry templates
  256. struct Point3{
  257. double x, y, z;
  258. Point3(double x = , double y = , double z = ) : x(x), y(y), z(z) {}
  259. };
  260. typedef Point3 Vector3;
  261. Vector3 operator + (Vector3 A, Vector3 B){
  262. return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
  263. }
  264. Vector3 operator - (Vector3 A, Vector3 B){
  265. return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
  266. }
  267. Vector3 operator * (Vector3 A, double p){
  268. return Vector3(A.x * p, A.y * p, A.z * p);
  269. }
  270. Vector3 operator / (Vector3 A, double p){
  271. return Vector3(A.x / p, A.y / p, A.z / p);
  272. }
  273. double Dot3(Vector3 A, Vector3 B){
  274. return A.x * B.x + A.y * B.y + A.z * B.z;
  275. }
  276. double Len3(Vector3 A){
  277. return sqrt(Dot3(A, A));
  278. }
  279. double Angle3(Vector3 A, Vector3 B){
  280. return acos(Dot3(A, B) / Len3(A) / Len3(B));
  281. }
  282. double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
  283. return abs(Dot3(p - p0, n));
  284. }
  285. Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
  286. return p - n * Dot3(p - p0, n);
  287. }
  288. Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
  289. Vector3 v = p2 - p1;
  290. double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
  291. return p1 + v * t;//if t in range [0, 1], intersection on segment
  292. }
  293. Vector3 Cross(Vector3 A, Vector3 B){
  294. return Vector3(A.y * B.z - A.z * B.y, A.z * B.x - A.x * B.z, A.x * B.y - A.y * B.x);
  295. }
  296. double Area3(Point3 A, Point3 B, Point3 C){
  297. return Len3(Cross(B - A, C - A));
  298. }
  299. class cmpt{
  300. public:
  301. bool operator () (const int &x, const int &y) const{
  302. return x > y;
  303. }
  304. };
  305.  
  306. int Rand(int x, int o){
  307. //if o set, return [1, x], else return [0, x - 1]
  308. if(!x) return ;
  309. int tem = (int)((double)rand() / RAND_MAX * x) % x;
  310. return o ? tem + : tem;
  311. }
  312. ////////////////////////////////////////////////////////////////////////////////////
  313. ////////////////////////////////////////////////////////////////////////////////////
  314. void data_gen(){
  315. srand(time());
  316. freopen("in.txt", "w", stdout);
  317. int times = ;
  318. printf("%d\n", times);
  319. while(times--){
  320. int r = Rand(, ), a = Rand(, ), c = Rand(, );
  321. int b = Rand(r, ), d = Rand(r, );
  322. int m = Rand(, ), n = Rand(m, );
  323. printf("%d %d %d %d %d %d %d\n", n, m, a, b, c, d, r);
  324. }
  325. }
  326.  
  327. struct cmpx{
  328. bool operator () (int x, int y) { return x > y; }
  329. };
  330. int debug = ;
  331. int dx[] = {-, , , };
  332. int dy[] = {, , -, };
  333. //-------------------------------------------------------------------------
  334. const int maxn = 5e2 + ;
  335. ll mt[maxn][maxn], res[maxn][maxn], tem[maxn][maxn];
  336. ll swp[maxn][maxn];
  337. ll P[maxn];
  338. ll ans[maxn];
  339. ll n, d, k, mod;
  340. void mt_power(ll p){
  341. clr(res, );
  342. FOR(i, , n - ) res[i][i] = % mod;
  343. memcpy(tem, mt, sizeof mt);
  344. while(p){
  345. if(p & ){
  346.  
  347. FOR(i, , ) FOR(j, , n - ){
  348. ll _tem = ;
  349. FOR(k, , n - ) _tem = (_tem + res[i][k] * tem[k][j] % mod) % mod;
  350. swp[i][j] = _tem;
  351. }
  352. FOR(i, , n - ) FOR(j, , n - ) swp[i][j] = swp[i - ][(j - + n) % n];
  353. memcpy(res, swp, sizeof swp);
  354. }
  355. p >>= ;
  356. FOR(i, , ) FOR(j, , n - ){
  357. ll _tem = ;
  358. FOR(k, , n - ) _tem = (_tem + tem[i][k] * tem[k][j] % mod) % mod;
  359. swp[i][j] = _tem;
  360. }
  361. FOR(i, , n - ) FOR(j, , n - ) swp[i][j] = swp[i - ][(j - + n) % n];
  362. memcpy(tem, swp, sizeof swp);
  363. }
  364. }
  365.  
  366. //-------------------------------------------------------------------------
  367. int main(){
  368. //data_gen(); return 0;
  369. //C(); return 0;
  370. debug = ;
  371. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  372. if(debug) freopen("in.txt", "r", stdin);
  373. //freopen("out.txt", "w", stdout);
  374. while(~scanf("%lld%lld%lld%lld", &n, &mod, &d, &k)){
  375. FOR(i, , n - ) scanf("%lld", &P[i]), P[i] %= mod;
  376. clr(mt, );
  377. FOR(i, , n - ){
  378. int l = i - d, r = i + d;
  379. FOR(j, l, r) mt[i][(j + n) % n] = ;
  380. }
  381. mt_power(k);
  382. FOR(i, , n - ){
  383. ans[i] = ;
  384. FOR(j, , n - ) ans[i] = (ans[i] + res[i][j] * P[j] % mod) % mod;
  385. }
  386. printf("%lld", ans[]);
  387. FOR(i, , n - ) printf(" %lld", ans[i]);
  388. printf("\n");
  389. }
  390. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  391. return ;
  392. }

code:

正确性证明:

我们不妨将满足第$0$行元素关于第$0$列对称(模意义下)且第$i + 1$行可由第$i$行循环右移一位得到的方阵称为$Z$矩阵。

我们试着证明若$A, B$均为$Z$矩阵,那么$AB$也是$Z$矩阵。

证明:

假设$A, B$均为$n \times n$矩阵,行列编号均为在模$n$意义下的值。

令$C=AB$,为了证明$C$为$Z$矩阵,只需证明$C(i, j)=C(i - 1, j - 1)$ 且$C(0, i) = C(0, -i)$。

由于$A$为$Z$矩阵,因此$A(i, j) = A(i - 1, j - 1) = A(0, j - i) = A(0, i - j) = A(j, i)$。

所以$Z$矩阵是对称阵。考虑如下等式:

$C(i,j)=\sum_{k=0}^{n-1}{A(i, k)B(k, j)}=\sum_{k=0}^{n-1}{A(0,k-i)B(0,j-k)}$

$=\sum_{k=-1}^{n-2}{A(0, k - i + 1)B(0,j - k - 1)}=\sum_{k=0}^{n-1}{A(0, k - i + 1)B(0,j - k - 1)}$

$\sum_{k=0}^{n-1}{A(i-1,k)B(k,j-1)}=C(i-1,j-1)$

此外:

$C(0,i)=\sum_{k=0}^{n-1}{A(0, k)B(k, i)}=\sum_{k=0}^{n-1}{A(0, k)B(0, i-k)}$

$=\sum_{k=0}^{n-1}{A(0, k)B(0, k-i)}=\sum_{k=0}^{n-1}{A(0, k)B(k,- i)}=C(0,-i)$

于是得知$C$也是$Z$矩阵。

LA 3704 Cellular Automaton的更多相关文章

  1. UVa 3704 Cellular Automaton(矩乘)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...

  2. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

  3. 【POJ】3150 Cellular Automaton(矩阵乘法+特殊的技巧)

    http://poj.org/problem?id=3150 这题裸的矩阵很容易看出,假设d=1,n=5那么矩阵是这样的 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...

  4. UVA 1386 - Cellular Automaton(循环矩阵)

    UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...

  5. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  6. UVA1386 【Cellular Automaton】题解

    题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...

  7. POJ 3150 Cellular Automaton(矩阵快速幂)

    Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...

  8. POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)

    A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...

  9. POJ 3150 Cellular Automaton(矩阵高速幂)

    题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...

随机推荐

  1. c++ DISALLOW_COPY_AND_ASSIGN

    Google C++编程规范 – 第三十二条 -<拷贝构造函数> [规范] 仅在确认需要的时候,才定义拷贝构造函数和赋值运算符:否则,请使用DISALLOW_COPY_AND_ASSIGN ...

  2. session 实现保存用户信息

    index.jsp <body> <div style="margin: 0 auto; width: 500px; text-align: center;"&g ...

  3. Nginx简单配置

    Nginx 配置文件结构如果你下载好啦,你的安装文件,不妨打开 conf 文件夹的 nginx.conf 文件,Nginx 服务器的基础配置,默认的配置也存放在此.在 nginx.conf 的注释符号 ...

  4. Python学习总结19:类(二)

    参考:http://python.jobbole.com/82308/ 继承和__slots__属性 1. 继承    在Python中,同时支持单继承与多继承,一般语法如下: class SubCl ...

  5. oracle,sqlserver,mysql 命令行 开启、关闭所需要的服务

    ORACLE需要开启的服务   需要启动的服务:   口令: 启动Oracle 11g服务: (下面的可以作为bat 脚本,直接运行便可以不用去自己去启动和关闭服务了.) @echo off @ EC ...

  6. Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别

    Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...

  7. 使用 GitHub / GitLab 的 Webhooks 进行网站自动化部署

    老早就想写这个话题了,今天正好有机会研究了一下 git 的自动化部署.最终做到的效果就是,每当有新的 commit push 到 master 分支的时候,就自动在测试/生产服务器上进行 git pu ...

  8. C语言 类型

    int 2个字节或4个字节 根据平台而定, -32,768 到 32,767 或 -2,147,483,648 到 2,147,483,647 unsigned int 2或4个字节    0到655 ...

  9. paper 83:前景检测算法_1(codebook和平均背景法)

    前景分割中一个非常重要的研究方向就是背景减图法,因为背景减图的方法简单,原理容易被想到,且在智能视频监控领域中,摄像机很多情况下是固定的,且背景也是基本不变或者是缓慢变换的,在这种场合背景减图法的应用 ...

  10. logstash配合filebeat监控tomcat日志

    环境:logstash版本:5.0.1&&filebeat 5.0.1 ABC为三台服务器.保证彼此tcp能够相互连接. Index服务器A - 接收BC两台服务器的tomcat日志 ...