UVA 1386 - Cellular Automaton

option=com_onlinejudge&Itemid=8&page=show_problem&category=489&problem=4132&mosmsg=Submission+received+with+ID+13911770" target="_blank" style="">题目链接

题意:给定一个n格的环,如今有个距离d。每次变化把环和他周围距离d以内的格子相加,结果mod m,问经过k次变换之后,环上的各个数字

思路:矩阵非常好想,每一个位置相应周围几个位置为1。其余位置为0,可是这个矩阵有500。有点大,直接n^3去求矩阵不太合适。然后观察发现这个矩阵是个循环矩阵,循环矩阵相乘的话,仅仅须要保存一行就可以。然后用n^2的时间就足够计算了

代码:

#include <stdio.h>
#include <string.h> const int N = 505; long long n, m, d, k, a[N]; struct mat {
long long v[N];
mat() {memset(v, 0, sizeof(v));}
mat operator * (mat c) {
mat ans;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans.v[i] = ((ans.v[i] + v[j] * c.v[(j - i + n) % n]) % m + m) % m;
}
}
return ans;
}
}; mat pow_mod(mat x, long long k) {
if (k == 1) return x;
mat sb = x * x;
mat ans = pow_mod(sb, k>>1);
if (k&1) ans = ans * x;
return ans;
} int main() {
while (~scanf("%lld%lld%lld%lld", &n, &m, &d, &k)) {
mat start;
for (int i = 0; i <= d; i++)
start.v[i] = 1;
for (int i = n - 1; i > n - 1 - d; i--)
start.v[i] = 1;
start = pow_mod(start, k);
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
for (int i = 0; i < n; i++) {
long long ans = 0;
for (int j = 0; j < n; j++)
ans = ((ans + a[j] * start.v[(j - i + n) % n]) % m + m) % m;
printf("%lld%c", ans, (i == n - 1 ? '\n' : ' '));
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA 1386 - Cellular Automaton(循环矩阵)的更多相关文章

  1. UVA 1386 Cellular Automaton

    矩阵快速幂. 样例是这样构造矩阵的: 矩阵很好构造,但是500*500*500*logk的复杂度显然是无法通过这题的. 其实本题构造出来的矩阵是一个循环矩阵,只需直到第一行或者第一列,即可直到整个矩阵 ...

  2. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

  3. POJ 3150 Cellular Automaton(矩阵快速幂)

    Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...

  4. 【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 ...

  5. UVa 3704 Cellular Automaton(矩乘)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...

  6. POJ 3150 Cellular Automaton(矩阵乘法+二分)

    题目链接 题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么. 思路 :下述来自here 首先来看一下Sample里的第一组 ...

  7. LA 3704 (矩阵快速幂 循环矩阵) Cellular Automaton

    将这n个格子看做一个向量,每次操作都是一次线性组合,即vn+1 = Avn,所求答案为Akv0 A是一个n*n的矩阵,比如当n=5,d=1的时候: 不难发现,A是个循环矩阵,也就是将某一行所有元素统一 ...

  8. POJ3150—Cellular Automaton(循环矩阵)

    题目链接:http://poj.org/problem?id=3150 题目意思:有n个数围成一个环,现在有一种变换,将所有距离第i(1<=i<=n)个数小于等于d的数加起来,对m取余,现 ...

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

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

随机推荐

  1. Collections在sort()简单分析法源

    Collections的sort方法代码: public static <T> void sort(List<T> list, Comparator<? super T& ...

  2. [ACM] hdu 1251 统计难题 (字典树)

    统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...

  3. jquery 如何动态添加、删除class样式方法介绍

    获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class 取与设置样式 获取class和设置class都可以使用attr()方法 ...

  4. 基于visual Studio2013解决面试题之1201链表去重

     题目

  5. Android Gradle Plugin指南(六)——高级构建定制

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Advanced-Build-Customization ...

  6. mysql学习之中的一个:mysql安装

    我用的时mac系统,本来想在mac系统上装一个,可是发现mac系统始终无法用password登入到本机server,很奇怪的问题(在stackflow上看了些回复,也没有找到原因),最后仅仅好装到虚拟 ...

  7. poj3278Catch That Cow(BFS)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37094   Accepted: 11466 ...

  8. 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序

    原文 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 在 Visual Studio 2010 中开发和部署 Windows Azure 应用程序 Jim ...

  9. perl post 请求带参数

    my $url='https://wenjinbao.winfae.com/business/dispatch_post.do?action=submitAdminLogin';    my $res ...

  10. [ACM] 九度OJ 1553 时钟

    时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1733 解决:656 题目描写叙述: 如图,给定随意时刻,求时针和分针的夹角(劣弧所相应的角). 输入: 输入包括多组測试数据.每组測试数 ...