POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 3504 Accepted: 1421
Case Time Limit: 2000MS
Description
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules that describe the new state of a cell based on the states of neighboring cells. The order of the cellular automaton is the number of cells it contains. Cells of the automaton of order n are numbered from 1 to n.
The order of the cell is the number of different values it may contain. Usually, values of a cell of order m are considered to be integer numbers from 0 to m − 1.
One of the most fundamental properties of a cellular automaton is the type of grid on which it is computed. In this problem we examine the special kind of cellular automaton — circular cellular automaton of order n with cells of order m. We will denote such kind of cellular automaton as n,m-automaton.
A distance between cells i and j in n,m-automaton is defined as min(|i − j|, n − |i − j|). A d-environment of a cell is the set of cells at a distance not greater than d.
On each d-step values of all cells are simultaneously replaced by new values. The new value of cell i after d-step is computed as a sum of values of cells belonging to the d-enviroment of the cell i modulo m.
The following picture shows 1-step of the 5,3-automaton.
The problem is to calculate the state of the n,m-automaton after k d-steps.
Input
The first line of the input file contains four integer numbers n, m, d, and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤ d < n⁄2 , 1 ≤ k ≤ 10 000 000). The second line contains n integer numbers from 0 to m − 1 — initial values of the automaton’s cells.
Output
Output the values of the n,m-automaton’s cells after k d-steps.
Sample Input
sample input #1
5 3 1 1
1 2 2 1 2
sample input #2
5 3 1 10
1 2 2 1 2
Sample Output
sample output #1
2 2 2 2 1
sample output #2
2 0 0 2 2
这道题目的矩阵好找,但是由于n比较大,用n*n的矩阵再加上快速幂,是O(n^3*log k) 回超时。观察矩阵,发现矩阵是一个循环矩阵,无论矩阵取多少次方,矩阵的每一行相当于第一行向后推了一步,所以说是循环矩阵,这样我们只要计算矩阵的第一行就可以知道矩阵的其他行,所以只开一维数组效率就是O(n^2log k)
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long int LL;
int n,m,d,k;
struct Node
{
LL a[505];
};
Node multiply(Node a,Node b)
{
Node c;
memset(c.a,0,sizeof(c.a));
for(int i=0;i<n;i++)
{
int cnt=(n-i)%n;
for(int j=0;j<n;j++)
{
(c.a[i]+=(a.a[j]*b.a[cnt++])%m)%=m;
if(cnt==n) cnt=0;
}
}
return c;
}
Node get(Node a,int x)
{
Node c;
memset(c.a,0,sizeof(c.a));
c.a[0]=1;
for(x;x;x>>=1)
{
if(x&1) c=multiply(c,a);
a=multiply(a,a);
}
return c;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&d,&k);
Node a;Node b;
memset(a.a,0,sizeof(a.a));
memset(b.a,0,sizeof(b.a));
for(int i=0;i<n;i++)
scanf("%lld",&b.a[i]);
a.a[0]=1;
for(int i=1;i<=d;i++)
a.a[i]=a.a[n-i]=1;
a=get(a,k);
a=multiply(b,a);
for(int i=0;i<n;i++)
if(i==n-1) printf("%lld\n",a.a[i]);
else printf("%lld ",a.a[i]);
return 0;
}
POJ 3150 Cellular Automaton(矩阵快速幂)的更多相关文章
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- POJ 3150 Cellular Automaton(矩阵高速幂)
题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...
- poj 3070 && nyoj 148 矩阵快速幂
poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...
- poj 3070 Fibonacci(矩阵快速幂,简单)
题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- poj 2778 AC自动机+矩阵快速幂
题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...
- Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)
题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- POJ 3734 Blocks (矩阵快速幂)
题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...
随机推荐
- SQL操作语句之查询及删除重复记录的方法
delete from 表 where id not in(select min(id) from 表 group by name ) //删除重复名字的记录 删除之前请用语句 select * fr ...
- mysql 常用功能
一.备份 mysqldump [OPTIONS] database [tables] http://www.blogjava.net/Alpha/archive/2007/08/10/135694.h ...
- Docker Python API 与 Docker Command
span.kw { color: #007020; font-weight: bold; } code > span.dt { color: #902000; } code > span. ...
- SQL server账号无法登陆
- 完整学习使用CSS动画【翻译】
注:原文有较大改动 使用keyframes, animation属性,例如timing, delay, play state, animation-count, iteration count, d ...
- java中调用数据库中的存储过程和函数
public static void main(String[] args) { Connection conn =getConnection(url,user, pwd); ...
- Redis-ha(sentinel)搭建
服务器描述:本次搭建是用来测试,所以是在一台服务器上搭建三个redis服务(一主两从) 服务角色 端口 Redis.conf名称 sentinel配置文件名称 sentinel端口 redis日志路径 ...
- 0047 Spring的AOP入门基础--切面组件--通知--切入点
AOP的全称是Aspect Oriented Programming,面向切面编程. 切面是什么呢,就是一个Java类.之所以叫切面,就是说在调用其他一些类的方法的某个时候(之前,之后,抛异常等),调 ...
- USART—串口通讯
本章中主要讲解的是串口异步通讯,异步通讯中由于没有时钟信号, 所以两个通讯设备之间需要约定好波特率,即每个码元的长度,以便对信号进行解码 . 串口通讯的一个数据包从起始信号开始,直到停止信号结束.数据 ...
- java:eclipse-tomcat 配置
计划开始学习java.第一步 1.在servers窗口中新建server 2.弹出的界面选择对应的tomcat版本 3.这里是关键,已存在的项目不要选择过去,否则最后生成的server配置无法修改se ...