POJ 3150 Cellular Automaton(矩阵乘法+二分)
题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么。
思路 :下述来自here
首先来看一下Sample里的第一组数据。
1 2 2 1 2
经过一次变换之后就成了
5 5 5 5 4
它的原理就是
a0 a1 a2 a3 a4
->
(a4+a0+a1) (a0+a1+a2) (a1+a2+a3) (a2+a3+a4) (a3+a4+a0)
如果用矩阵相乘来描述,那就可以表述为1xN和NxN的矩阵相乘,结果仍为1xN矩阵
a = 1 2 2 1 2
b =
1 1 0 0 1
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
1 0 0 1 1
a * b = 5 5 5 5 4
所以最终结果就是:a * (b^k)
线性代数不合格的同鞋表示压力很大。。
对一个NxN矩阵求k次方,而且这个k很大,N也不小,怎么办?
所以有高手观察到了,这个矩阵长得有点特殊,可以找到一些规律:
b^1 =
[1, 1, 0, 0, 1]
[1, 1, 1, 0, 0]
[0, 1, 1, 1, 0]
[0, 0, 1, 1, 1]
[1, 0, 0, 1, 1]
b^2 =
[3, 2, 1, 1, 2]
[2, 3, 2, 1, 1]
[1, 2, 3, 2, 1]
[1, 1, 2, 3, 2]
[2, 1, 1, 2, 3]
b^3 =
[7, 6, 4, 4, 6]
[6, 7, 6, 4, 4]
[4, 6, 7, 6, 4]
[4, 4, 6, 7, 6]
[6, 4, 4, 6, 7]
b^4 =
[19, 17, 14, 14, 17]
[17, 19, 17, 14, 14]
[14, 17, 19, 17, 14]
[14, 14, 17, 19, 17]
[17, 14, 14, 17, 19]
发现神马没有。就是无论是b的几次幂,都符合A[i][j] = A[i-1][j-1]
高手说是这样推倒出来地:
““”
利用矩阵A,B具有a[i][j]=A[i-1][j-1],B[i][j]=B[i-1][j-1](i-1<0则表示i-1+n,j-1<0则表示j-1+n)
我们可以得出矩阵C=a*b也具有这个性质
C[i][j]=sum(A[i][t]*B[t][j])=sum(A[i-1][t-1],B[t-1][j-1])=sum(A[i-1][t],B[t][j-1])=C[i-1][j-1]
“”“
这样就可以开一个N大小的数组来存放每次计算的结果了。而没必要用NxN。
N的问题解决了,但是k还是很大,怎么办?
这时候可以用二分法来求b^k
b^k = b^1 * b^4 * b^16 。。。
//
#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long using namespace std ; int n,m , d , k ;
LL a[] ,b[] ;
void multi(LL *c,LL *d)
{
LL x[] ;
for(int i = ; i < n ; i++)
{
x[i] = ;
for(int j = ; j < n ; j++)
x[i] += c[j] * d[i >= j ? (i - j) : (n + i - j)] ;//防止是负数,形成环
}
for(int i = ; i < n ; i++)
d[i] = x[i] % m ;
}
int main()
{
while(cin >> n >> m >> d >> k ){
for(int i = ; i < n ; i++)
cin >> a[i] ;
b[] = ;
for(int i = ; i <= d ; i++)
b[i] = b[n - i] = ;
while(k)
{
if(k & )//奇数
multi(b,a) ;
multi(b,b) ;
k >>= ;
}
for(int i = ; i < n ; i++)
if(i == n-) printf("%I64d\n",a[i]) ;
else printf("%I64d ",a[i]) ;
}
return ;
}
计算过程中,必定会出现数字大于M的情况。
切记 x*y = (x%M)*(y%M)
POJ 3150 Cellular Automaton(矩阵乘法+二分)的更多相关文章
- [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 3150 Cellular Automaton --矩阵快速幂及优化
题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...
- 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
首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...
- 【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 ...
- BZOJ 4180: 字符串计数 后缀自动机 + 矩阵乘法 + 二分(神题)
Description SD有一名神犇叫做Oxer,他觉得字符串的题目都太水了,于是便出了一道题来虐蒟蒻yts1999. 他给出了一个字符串T,字符串T中有且仅有4种字符 'A', 'B', 'C ...
- POJ 2778 (AC自动机+矩阵乘法)
POJ 2778 DNA Sequence Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的 ...
随机推荐
- lvs keepalived 安装配置详解
前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较.F5相当的贵,真不是一般企业能负担的起的.负载均衡软件也用过不少,nginx,apache,hapro ...
- CAD字体显示错乱问题解决方案
最近这两天一直在画竣工图,用CAD用得挺多的,所以老是发现一些问题.今天在打开别人发过来的图纸时,我看到竟然还有钢筋符号无法显示…… 像这种问题的解决,据我所知就两种方法: 一.替换使用的字体 首先选 ...
- N!大整数阶乘问题
问题:求N!阶乘,1<=N<10000 思路:windows下面visual 6.0中c一个整型占4个字节(自己可以try一下,printf("%d", sizeof( ...
- c语言中static 用法总结(转)
惨痛教训: 假设在test.h中定义了一个static bool g_test=false; 若test1.c和test2.c都包含test.h,则test1.c和test2.c分别生成两份g_tes ...
- Notes of the scrum meeting(11/3)
meeting time:19:30~20:00p.m.,November 3th,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 5727310284062720
2014-05-06 14:04 题目链接 原题: given an 2D matrix M, is filled either using X or O, you need to find the ...
- 22、DDMS(转载)
本文是转载,出处为http://www.xuebuyuan.com/1291595.html 如需删除本文,请私信我,谢谢 DDMS DDMS是一款Google* 提供的应用,可作为独立的工具运行,也 ...
- ngcordova 监控网络制式改变
ngcordova 监控网络制式改变 keywords cordova,phonegap,ionic,network,网络制式 API参考 http://ngcordova.com/docs/plug ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...