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 ...
随机推荐
- jq 遍历元素 筛选
var productInfo = $.grep(productData, function (value) { return value.productInfo.pId == favourite.p ...
- QT Unexpected CDB exit 问题的解决办法
行QT进行debug时,提示 Unexpected CDB exit ,The CBD process terminated.. QtCreator 默认是没有调试器的,因此需要用户额外安装. win ...
- C++的多态例子
1.多态的例子 题目: 某小型公司,主要有四类员工(Employee):经理(Manager).技术人员(Technician).销售经理(SalesManager)和推销员(SalesMan).现在 ...
- html5_storage存取实例
<script src="jquery-1.8.3.js"></script><script>function set(){ var tt ...
- Mongodb更新数组$pull修饰符
http://blog.csdn.net/yaomingyang/article/details/78701643 一.$pull修饰符会删除掉数组中符合条件的元素,使用的格式是: { $pull: ...
- [k8s]k8s api-server启动systemd参数分析
默认2个参数就可以启动(必需) kube-apiserver \ --service-cluster-ip-range=10.254.0.0/16 \ --etcd-servers=http://19 ...
- python学习之sys模块
查看python的版本 >>> sys.version_info[] sys.argv 列表对象,传入模块参数的都会放入列表中. #-*- coding: utf-8 -*- # i ...
- php 给图片增加背景平铺水印代码
如果你想利用php 给图片增加背景平铺水印效果话,必须利用php的一个插件来实例,就是利用imagick,他可以给图片增加背景平铺水印效果哦,下面我们提供一款实例代码. 如果你想利用php教程 给图片 ...
- linux 查看java进程
ps -ef|grep java 1. jps 或 ps –ef|grepjava (获取PID)
- cp/scp命令详解
cp:拷贝命令 用法: cp [参数] source dest cp [参数] source ... directory 说明:将一个档案拷贝至另一个档案,或数个档案拷贝到另一目录 参数: -a 尽可 ...