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))$,可以通过。
- #include <algorithm>
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <queue>
- #include <map>
- #include <set>
- #include <ctime>
- #include <cmath>
- #include <iostream>
- #include <assert.h>
- #define PI acos(-1.)
- #pragma comment(linker, "/STACK:102400000,102400000")
- #define max(a, b) ((a) > (b) ? (a) : (b))
- #define min(a, b) ((a) < (b) ? (a) : (b))
- #define mp make_pair
- #define st first
- #define nd second
- #define keyn (root->ch[1]->ch[0])
- #define lson (u << 1)
- #define rson (u << 1 | 1)
- #define pii pair<int, int>
- #define pll pair<ll, ll>
- #define pb push_back
- #define type(x) __typeof(x.begin())
- #define foreach(i, j) for(type(j)i = j.begin(); i != j.end(); i++)
- #define FOR(i, s, t) for(int i = (s); i <= (t); i++)
- #define ROF(i, t, s) for(int i = (t); i >= (s); i--)
- #define dbg(x) cout << x << endl
- #define dbg2(x, y) cout << x << " " << y << endl
- #define clr(x, i) memset(x, (i), sizeof(x))
- #define maximize(x, y) x = max((x), (y))
- #define minimize(x, y) x = min((x), (y))
- #define low_bit(x) ((x) & (-x))
- using namespace std;
- typedef long long ll;
- const int int_inf = 0x3f3f3f3f;
- const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
- const int INT_INF = (int)((1ll << ) - );
- const double double_inf = 1e30;
- const double eps = 1e-;
- typedef unsigned long long ul;
- inline int readint(){
- int x;
- scanf("%d", &x);
- return x;
- }
- inline int readstr(char *s){
- scanf("%s", s);
- return strlen(s);
- }
- //Here goes 2d geometry templates
- struct Point{
- double x, y;
- Point(double x = , double y = ) : x(x), y(y) {}
- };
- typedef Point Vector;
- Vector operator + (Vector A, Vector B){
- return Vector(A.x + B.x, A.y + B.y);
- }
- Vector operator - (Point A, Point B){
- return Vector(A.x - B.x, A.y - B.y);
- }
- Vector operator * (Vector A, double p){
- return Vector(A.x * p, A.y * p);
- }
- Vector operator / (Vector A, double p){
- return Vector(A.x / p, A.y / p);
- }
- bool operator < (const Point& a, const Point& b){
- return a.x < b.x || (a.x == b.x && a.y < b.y);
- }
- int dcmp(double x){
- if(abs(x) < eps) return ;
- return x < ? - : ;
- }
- bool operator == (const Point& a, const Point& b){
- return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
- }
- double Dot(Vector A, Vector B){
- return A.x * B.x + A.y * B.y;
- }
- double Len(Vector A){
- return sqrt(Dot(A, A));
- }
- double Angle(Vector A, Vector B){
- return acos(Dot(A, B) / Len(A) / Len(B));
- }
- double Cross(Vector A, Vector B){
- return A.x * B.y - A.y * B.x;
- }
- double Area2(Point A, Point B, Point C){
- return Cross(B - A, C - A);
- }
- Vector Rotate(Vector A, double rad){
- //rotate counterclockwise
- return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
- }
- Vector Normal(Vector A){
- double L = Len(A);
- return Vector(-A.y / L, A.x / L);
- }
- void Normallize(Vector &A){
- double L = Len(A);
- A.x /= L, A.y /= L;
- }
- Point GetLineIntersection(Point P, Vector v, Point Q, Vector w){
- Vector u = P - Q;
- double t = Cross(w, u) / Cross(v, w);
- return P + v * t;
- }
- double DistanceToLine(Point P, Point A, Point B){
- Vector v1 = B - A, v2 = P - A;
- return abs(Cross(v1, v2)) / Len(v1);
- }
- double DistanceToSegment(Point P, Point A, Point B){
- if(A == B) return Len(P - A);
- Vector v1 = B - A, v2 = P - A, v3 = P - B;
- if(dcmp(Dot(v1, v2)) < ) return Len(v2);
- else if(dcmp(Dot(v1, v3)) > ) return Len(v3);
- else return abs(Cross(v1, v2)) / Len(v1);
- }
- Point GetLineProjection(Point P, Point A, Point B){
- Vector v = B - A;
- return A + v * (Dot(v, P - A) / Dot(v, v));
- }
- bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
- //Line1:(a1, a2) Line2:(b1,b2)
- double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
- c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
- return dcmp(c1) * dcmp(c2) < && dcmp(c3) * dcmp(c4) < ;
- }
- bool OnSegment(Point p, Point a1, Point a2){
- return dcmp(Cross(a1 - p, a2 - p)) == && dcmp(Dot(a1 - p, a2 -p)) < ;
- }
- Vector GetBisector(Vector v, Vector w){
- Normallize(v), Normallize(w);
- return Vector((v.x + w.x) / , (v.y + w.y) / );
- }
- bool OnLine(Point p, Point a1, Point a2){
- Vector v1 = p - a1, v2 = a2 - a1;
- double tem = Cross(v1, v2);
- return dcmp(tem) == ;
- }
- struct Line{
- Point p;
- Vector v;
- Point point(double t){
- return Point(p.x + t * v.x, p.y + t * v.y);
- }
- Line(Point p, Vector v) : p(p), v(v) {}
- };
- struct Circle{
- Point c;
- double r;
- Circle(Point c, double r) : c(c), r(r) {}
- Circle(int x, int y, int _r){
- c = Point(x, y);
- r = _r;
- }
- Point point(double a){
- return Point(c.x + cos(a) * r, c.y + sin(a) * r);
- }
- };
- int GetLineCircleIntersection(Line L, Circle C, double &t1, double& t2, vector<Point>& sol){
- double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
- double e = a * a + c * c, f = * (a * b + c * d), g = b * b + d * d - C.r * C.r;
- double delta = f * f - * e * g;
- if(dcmp(delta) < ) return ;
- if(dcmp(delta) == ){
- t1 = t2 = -f / ( * e); sol.pb(L.point(t1));
- return ;
- }
- t1 = (-f - sqrt(delta)) / ( * e); sol.pb(L.point(t1));
- t2 = (-f + sqrt(delta)) / ( * e); sol.pb(L.point(t2));
- return ;
- }
- double angle(Vector v){
- return atan2(v.y, v.x);
- //(-pi, pi]
- }
- int GetCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& sol){
- double d = Len(C1.c - C2.c);
- if(dcmp(d) == ){
- if(dcmp(C1.r - C2.r) == ) return -; //two circle duplicates
- return ; //two circles share identical center
- }
- if(dcmp(C1.r + C2.r - d) < ) return ; //too close
- if(dcmp(abs(C1.r - C2.r) - d) > ) return ; //too far away
- double a = angle(C2.c - C1.c); // angle of vector(C1, C2)
- double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / ( * C1.r * d));
- Point p1 = C1.point(a - da), p2 = C1.point(a + da);
- sol.pb(p1);
- if(p1 == p2) return ;
- sol.pb(p2);
- return ;
- }
- int GetPointCircleTangents(Point p, Circle C, Vector* v){
- Vector u = C.c - p;
- double dist = Len(u);
- if(dist < C.r) return ;//p is inside the circle, no tangents
- else if(dcmp(dist - C.r) == ){
- // p is on the circles, one tangent only
- v[] = Rotate(u, PI / );
- return ;
- }else{
- double ang = asin(C.r / dist);
- v[] = Rotate(u, -ang);
- v[] = Rotate(u, +ang);
- return ;
- }
- }
- int GetCircleCircleTangents(Circle A, Circle B, Point* a, Point* b){
- //a[i] store point of tangency on Circle A of tangent i
- //b[i] store point of tangency on Circle B of tangent i
- //six conditions is in consideration
- int cnt = ;
- if(A.r < B.r) { swap(A, B); swap(a, b); }
- 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);
- int rdiff = A.r - B.r;
- int rsum = A.r + B.r;
- if(d2 < rdiff * rdiff) return ; // one circle is inside the other
- double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x);
- if(d2 == && A.r == B.r) return -; // two circle duplicates
- if(d2 == rdiff * rdiff){ // internal tangency
- a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++;
- return ;
- }
- double ang = acos((A.r - B.r) / sqrt(d2));
- a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang);
- a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang);
- if(d2 == rsum * rsum){
- //one internal tangent
- a[cnt] = A.point(base);
- b[cnt++] = B.point(base + PI);
- }else if(d2 > rsum * rsum){
- //two internal tangents
- double ang = acos((A.r + B.r) / sqrt(d2));
- a[cnt] = A.point(base + ang); b[cnt++] = B.point(base + ang + PI);
- a[cnt] = A.point(base - ang); b[cnt++] = B.point(base - ang + PI);
- }
- return cnt;
- }
- Point ReadPoint(){
- double x, y;
- scanf("%lf%lf", &x, &y);
- return Point(x, y);
- }
- Circle ReadCircle(){
- double x, y, r;
- scanf("%lf%lf%lf", &x, &y, &r);
- return Circle(x, y, r);
- }
- //Here goes 3d geometry templates
- struct Point3{
- double x, y, z;
- Point3(double x = , double y = , double z = ) : x(x), y(y), z(z) {}
- };
- typedef Point3 Vector3;
- Vector3 operator + (Vector3 A, Vector3 B){
- return Vector3(A.x + B.x, A.y + B.y, A.z + B.z);
- }
- Vector3 operator - (Vector3 A, Vector3 B){
- return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
- }
- Vector3 operator * (Vector3 A, double p){
- return Vector3(A.x * p, A.y * p, A.z * p);
- }
- Vector3 operator / (Vector3 A, double p){
- return Vector3(A.x / p, A.y / p, A.z / p);
- }
- double Dot3(Vector3 A, Vector3 B){
- return A.x * B.x + A.y * B.y + A.z * B.z;
- }
- double Len3(Vector3 A){
- return sqrt(Dot3(A, A));
- }
- double Angle3(Vector3 A, Vector3 B){
- return acos(Dot3(A, B) / Len3(A) / Len3(B));
- }
- double DistanceToPlane(const Point3& p, const Point3 &p0, const Vector3& n){
- return abs(Dot3(p - p0, n));
- }
- Point3 GetPlaneProjection(const Point3 &p, const Point3 &p0, const Vector3 &n){
- return p - n * Dot3(p - p0, n);
- }
- Point3 GetLinePlaneIntersection(Point3 p1, Point3 p2, Point3 p0, Vector3 n){
- Vector3 v = p2 - p1;
- double t = (Dot3(n, p0 - p1) / Dot3(n, p2 - p1));
- return p1 + v * t;//if t in range [0, 1], intersection on segment
- }
- Vector3 Cross(Vector3 A, Vector3 B){
- 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);
- }
- double Area3(Point3 A, Point3 B, Point3 C){
- return Len3(Cross(B - A, C - A));
- }
- class cmpt{
- public:
- bool operator () (const int &x, const int &y) const{
- return x > y;
- }
- };
- int Rand(int x, int o){
- //if o set, return [1, x], else return [0, x - 1]
- if(!x) return ;
- int tem = (int)((double)rand() / RAND_MAX * x) % x;
- return o ? tem + : tem;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////
- void data_gen(){
- srand(time());
- freopen("in.txt", "w", stdout);
- int times = ;
- printf("%d\n", times);
- while(times--){
- int r = Rand(, ), a = Rand(, ), c = Rand(, );
- int b = Rand(r, ), d = Rand(r, );
- int m = Rand(, ), n = Rand(m, );
- printf("%d %d %d %d %d %d %d\n", n, m, a, b, c, d, r);
- }
- }
- struct cmpx{
- bool operator () (int x, int y) { return x > y; }
- };
- int debug = ;
- int dx[] = {-, , , };
- int dy[] = {, , -, };
- //-------------------------------------------------------------------------
- const int maxn = 5e2 + ;
- ll mt[maxn][maxn], res[maxn][maxn], tem[maxn][maxn];
- ll swp[maxn][maxn];
- ll P[maxn];
- ll ans[maxn];
- ll n, d, k, mod;
- void mt_power(ll p){
- clr(res, );
- FOR(i, , n - ) res[i][i] = % mod;
- memcpy(tem, mt, sizeof mt);
- while(p){
- if(p & ){
- FOR(i, , ) FOR(j, , n - ){
- ll _tem = ;
- FOR(k, , n - ) _tem = (_tem + res[i][k] * tem[k][j] % mod) % mod;
- swp[i][j] = _tem;
- }
- FOR(i, , n - ) FOR(j, , n - ) swp[i][j] = swp[i - ][(j - + n) % n];
- memcpy(res, swp, sizeof swp);
- }
- p >>= ;
- FOR(i, , ) FOR(j, , n - ){
- ll _tem = ;
- FOR(k, , n - ) _tem = (_tem + tem[i][k] * tem[k][j] % mod) % mod;
- swp[i][j] = _tem;
- }
- FOR(i, , n - ) FOR(j, , n - ) swp[i][j] = swp[i - ][(j - + n) % n];
- memcpy(tem, swp, sizeof swp);
- }
- }
- //-------------------------------------------------------------------------
- int main(){
- //data_gen(); return 0;
- //C(); return 0;
- debug = ;
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if(debug) freopen("in.txt", "r", stdin);
- //freopen("out.txt", "w", stdout);
- while(~scanf("%lld%lld%lld%lld", &n, &mod, &d, &k)){
- FOR(i, , n - ) scanf("%lld", &P[i]), P[i] %= mod;
- clr(mt, );
- FOR(i, , n - ){
- int l = i - d, r = i + d;
- FOR(j, l, r) mt[i][(j + n) % n] = ;
- }
- mt_power(k);
- FOR(i, , n - ){
- ans[i] = ;
- FOR(j, , n - ) ans[i] = (ans[i] + res[i][j] * P[j] % mod) % mod;
- }
- printf("%lld", ans[]);
- FOR(i, , n - ) printf(" %lld", ans[i]);
- printf("\n");
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- return ;
- }
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的更多相关文章
- UVa 3704 Cellular Automaton(矩乘)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...
- UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)
题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...
- 【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 ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- UVA1386 【Cellular Automaton】题解
题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...
- POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
- POJ 3150 Cellular Automaton(矩阵高速幂)
题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...
随机推荐
- c++ DISALLOW_COPY_AND_ASSIGN
Google C++编程规范 – 第三十二条 -<拷贝构造函数> [规范] 仅在确认需要的时候,才定义拷贝构造函数和赋值运算符:否则,请使用DISALLOW_COPY_AND_ASSIGN ...
- session 实现保存用户信息
index.jsp <body> <div style="margin: 0 auto; width: 500px; text-align: center;"&g ...
- Nginx简单配置
Nginx 配置文件结构如果你下载好啦,你的安装文件,不妨打开 conf 文件夹的 nginx.conf 文件,Nginx 服务器的基础配置,默认的配置也存放在此.在 nginx.conf 的注释符号 ...
- Python学习总结19:类(二)
参考:http://python.jobbole.com/82308/ 继承和__slots__属性 1. 继承 在Python中,同时支持单继承与多继承,一般语法如下: class SubCl ...
- oracle,sqlserver,mysql 命令行 开启、关闭所需要的服务
ORACLE需要开启的服务 需要启动的服务: 口令: 启动Oracle 11g服务: (下面的可以作为bat 脚本,直接运行便可以不用去自己去启动和关闭服务了.) @echo off @ EC ...
- Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别
Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...
- 使用 GitHub / GitLab 的 Webhooks 进行网站自动化部署
老早就想写这个话题了,今天正好有机会研究了一下 git 的自动化部署.最终做到的效果就是,每当有新的 commit push 到 master 分支的时候,就自动在测试/生产服务器上进行 git pu ...
- C语言 类型
int 2个字节或4个字节 根据平台而定, -32,768 到 32,767 或 -2,147,483,648 到 2,147,483,647 unsigned int 2或4个字节 0到655 ...
- paper 83:前景检测算法_1(codebook和平均背景法)
前景分割中一个非常重要的研究方向就是背景减图法,因为背景减图的方法简单,原理容易被想到,且在智能视频监控领域中,摄像机很多情况下是固定的,且背景也是基本不变或者是缓慢变换的,在这种场合背景减图法的应用 ...
- logstash配合filebeat监控tomcat日志
环境:logstash版本:5.0.1&&filebeat 5.0.1 ABC为三台服务器.保证彼此tcp能够相互连接. Index服务器A - 接收BC两台服务器的tomcat日志 ...