题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值。

解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有讲,大概为:

[1 1 0 .. 0 1]

[1 1 1 .. .. 0]

...

[1 1 .. .. .. 1]  的循环矩阵,可以证明,循环矩阵的乘积还是循环矩阵,且循环矩阵的性质: a[i][j] = a[i-1][j-1] (循环的) ,所以,我们每次矩阵相乘只需要算出第一行,余下的不需要通过矩阵乘法来算出,直接根据规律推出,这样,矩阵乘法的复杂度就降到了O(n^2),加上快速幂,总复杂度O(n^2log(k))。

注意:中间相乘的时候a[i][k]*b[k][j]可能会超过int范围,要加一个long long,否则会WA.

代码:(6000+ ms  也是醉了。。)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define SMod m
#define ll long long
using namespace std; int n,m,k,d;
struct Matrix
{
int m[][];
Matrix()
{
memset(m,,sizeof(m));
for(int i=;i<=n;i++)
m[i][i] = ;
}
}; Matrix Mul(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(j=;j<=n;j++)
{
res.m[][j] = ;
for(k=;k<=n;k++)
res.m[][j] = (res.m[][j]+(ll)a.m[][k]*b.m[k][j]%SMod + SMod)%SMod;
}
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
res.m[i][j] = res.m[i-][j-];
res.m[i][] = res.m[i-][n];
}
return res;
} Matrix fastm(Matrix a,int b)
{
Matrix res;
while(b)
{
if(b&)
res = Mul(res,a);
a = Mul(a,a);
b >>= ;
}
return res;
} Matrix Muti(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(i=;i<=n;i++)
{
res.m[i][] = ;
for(k=;k<=n;k++)
res.m[i][] = (res.m[i][]+(ll)a.m[i][k]*b.m[k][]%SMod + SMod)%SMod;
}
return res;
} int main()
{
int i,j;
while(scanf("%d%d%d%d",&n,&m,&d,&k)!=EOF)
{
Matrix R;
memset(R.m,,sizeof(R.m));
for(i=;i<=n;i++)
scanf("%d",&R.m[i][]),R.m[i][]%=SMod;
Matrix A;
for(i=;i<=d+;i++)
A.m[][i] = ;
for(i=n;i>=n-d+;i--)
A.m[][i] = ;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
A.m[i][j] = A.m[i-][j-];
A.m[i][] = A.m[i-][n];
}
/*for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(min(abs(i-j),n-abs(i-j)) <= d)
A.m[j][i] = 1;
else
A.m[j][i] = 0;
}
}*/
Matrix ans = fastm(A,k);
ans = Muti(ans,R);
for(i=;i<=n;i++)
printf("%d%c",ans.m[i][]%m,i==n?'\n':' ');
}
return ;
}

POJ 3150 Cellular Automaton --矩阵快速幂及优化的更多相关文章

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

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

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

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

  3. poj 3070 && nyoj 148 矩阵快速幂

    poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...

  4. poj 3070 Fibonacci(矩阵快速幂,简单)

    题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...

  5. POJ 3070 Fibonacci(矩阵快速幂)

    题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...

  6. poj 2778 AC自动机+矩阵快速幂

    题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...

  7. Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)

    题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...

  8. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  9. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

随机推荐

  1. No.012:Integer to Roman

    题目: Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from ...

  2. git 使用笔记(一)

    1. 环境介绍 windows10 2.使用 2.1 安装git for windows 2.2 创建一个文件夹, 开始git管理 2.3 查看该目录,包括隐藏文件 2.4 把testgit.txt添 ...

  3. php 7 windows redis 扩展

    搜了一圈也没找到redis 对于 php 7 windows 扩展,最后还是在apache lounge论坛找到了php7全扩展包 本人把里面的扩展全进行了上传(下载见本文底部) 在这里主要讲讲 ph ...

  4. php正规则表达式学习笔记(几个常用函数的区别)

    preg_mache()函数和 preg_mache_all()函数的区别: preg_mache()只会匹配规则中的字符一次, preg_mache_all()会匹配符合条件的所有字符! 例子对比: ...

  5. cas与NGINX整合(转)

    CAS (5) -- Nginx代理模式下浏览器访问CAS服务器配置详解 标签: 服务器 2015-12-18 15:04 1633人阅读 评论(0) 收藏 举报  分类: 网络(61)    目录( ...

  6. Python for循环内部实现的一个sample

    #!/usr/bin/env python # -*- coding: utf-8 -*- it = iter([1,2,3,4,5]) while True: try: x = next(it) p ...

  7. 以Self Host的方式来寄宿Web API

    Common类及实体定义.Web API的定义请参见我的上一篇文章:以Web Host的方式来寄宿Web API. 一.以Self Host寄宿需要新建一个Console控制台项目(SelfHost) ...

  8. mysql root用户kill connection报ERROR 1095 (HY000): You are not owner of thread N

    今日某系统mysql root用户kill connection时报ERROR 1095 (HY000): You are not owner of thread N 按说通过root用户具有supe ...

  9. C#中List<T>对象的深度拷贝问题

    一.List<T>对象中的T是值类型的情况(int 类型等) 对于值类型的List直接用以下方法就可以复制: List<T> oldList = new List<T&g ...

  10. 本地Git服务器的搭建及使用

    本地Git服务器的搭建及使用 Git本地服务器环境搭建 搭建好的本地git服务器的局域网ip是192.168.1.188,用户名是RSCSVN 教程链接:http://blog.csdn.net/cc ...