LA 3704 Cellular Automaton
题意概述:
等价地,本题可以转化为下面的问题:
考虑$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))$,可以通过。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 << 31) - 1); 40 const double double_inf = 1e30; 41 const double eps = 1e-14; 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 = 0, double y = 0) : 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 0; 75 return x < 0 ? -1 : 1; 76 } 77 bool operator == (const Point& a, const Point& b){ 78 return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; 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)) < 0) return Len(v2); 120 else if(dcmp(Dot(v1, v3)) > 0) 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) < 0 && dcmp(c3) * dcmp(c4) < 0; 132 } 133 bool OnSegment(Point p, Point a1, Point a2){ 134 return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 -p)) < 0; 135 } 136 Vector GetBisector(Vector v, Vector w){ 137 Normallize(v), Normallize(w); 138 return Vector((v.x + w.x) / 2, (v.y + w.y) / 2); 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) == 0; 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 = 2 * (a * b + c * d), g = b * b + d * d - C.r * C.r; 169 double delta = f * f - 4 * e * g; 170 if(dcmp(delta) < 0) return 0; 171 if(dcmp(delta) == 0){ 172 t1 = t2 = -f / (2 * e); sol.pb(L.point(t1)); 173 return 1; 174 } 175 t1 = (-f - sqrt(delta)) / (2 * e); sol.pb(L.point(t1)); 176 t2 = (-f + sqrt(delta)) / (2 * e); sol.pb(L.point(t2)); 177 return 2; 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) == 0){ 186 if(dcmp(C1.r - C2.r) == 0) return -1; //two circle duplicates 187 return 0; //two circles share identical center 188 } 189 if(dcmp(C1.r + C2.r - d) < 0) return 0; //too close 190 if(dcmp(abs(C1.r - C2.r) - d) > 0) return 0; //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) / (2 * C1.r * d)); 193 Point p1 = C1.point(a - da), p2 = C1.point(a + da); 194 sol.pb(p1); 195 if(p1 == p2) return 1; 196 sol.pb(p2); 197 return 2; 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 0;//p is inside the circle, no tangents 203 else if(dcmp(dist - C.r) == 0){ 204 // p is on the circles, one tangent only 205 v[0] = Rotate(u, PI / 2); 206 return 1; 207 }else{ 208 double ang = asin(C.r / dist); 209 v[0] = Rotate(u, -ang); 210 v[1] = Rotate(u, +ang); 211 return 2; 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 = 0; 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 0; // 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 == 0 && A.r == B.r) return -1; // two circle duplicates 226 if(d2 == rdiff * rdiff){ // internal tangency 227 a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++; 228 return 1; 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 = 0, double y = 0, double z = 0) : 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 0; 309 int tem = (int)((double)rand() / RAND_MAX * x) % x; 310 return o ? tem + 1 : tem; 311 } 312 //////////////////////////////////////////////////////////////////////////////////// 313 //////////////////////////////////////////////////////////////////////////////////// 314 void data_gen(){ 315 srand(time(0)); 316 freopen("in.txt", "w", stdout); 317 int times = 100; 318 printf("%d\n", times); 319 while(times--){ 320 int r = Rand(1000, 1), a = Rand(1000, 1), c = Rand(1000, 1); 321 int b = Rand(r, 1), d = Rand(r, 1); 322 int m = Rand(100, 1), n = Rand(m, 1); 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 = 1; 331 int dx[] = {-1, 1, 0, 0}; 332 int dy[] = {0, 0, -1, 1}; 333 //------------------------------------------------------------------------- 334 const int maxn = 5e2 + 10; 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, 0); 342 FOR(i, 0, n - 1) res[i][i] = 1 % mod; 343 memcpy(tem, mt, sizeof mt); 344 while(p){ 345 if(p & 1){ 346 347 FOR(i, 0, 0) FOR(j, 0, n - 1){ 348 ll _tem = 0; 349 FOR(k, 0, n - 1) _tem = (_tem + res[i][k] * tem[k][j] % mod) % mod; 350 swp[i][j] = _tem; 351 } 352 FOR(i, 1, n - 1) FOR(j, 0, n - 1) swp[i][j] = swp[i - 1][(j - 1 + n) % n]; 353 memcpy(res, swp, sizeof swp); 354 } 355 p >>= 1; 356 FOR(i, 0, 0) FOR(j, 0, n - 1){ 357 ll _tem = 0; 358 FOR(k, 0, n - 1) _tem = (_tem + tem[i][k] * tem[k][j] % mod) % mod; 359 swp[i][j] = _tem; 360 } 361 FOR(i, 1, n - 1) FOR(j, 0, n - 1) swp[i][j] = swp[i - 1][(j - 1 + 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 = 0; 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, 0, n - 1) scanf("%lld", &P[i]), P[i] %= mod; 376 clr(mt, 0); 377 FOR(i, 0, n - 1){ 378 int l = i - d, r = i + d; 379 FOR(j, l, r) mt[i][(j + n) % n] = 1; 380 } 381 mt_power(k); 382 FOR(i, 0, n - 1){ 383 ans[i] = 0; 384 FOR(j, 0, n - 1) ans[i] = (ans[i] + res[i][j] * P[j] % mod) % mod; 385 } 386 printf("%lld", ans[0]); 387 FOR(i, 1, n - 1) printf(" %lld", ans[i]); 388 printf("\n"); 389 } 390 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 391 return 0; 392 }
正确性证明:
我们不妨将满足第$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$矩阵。