FZU Problem 1692 Key problem(循环矩阵)

循环矩阵,这里有解说:http://wenku.baidu.com/link?

url=zcJ-sxrj0QDqzz8xCnHTnB7gxjoNRyOZzS4_4ZA22c8Bs9inYn6vVkqTVr_w-riLa8oRnYA9SRcCZ9f4UciCUNGeNAG4dCGclYRPS18YLGa

推出第一层以下依据性质就能够得到。

Problem 1692 Key problem

Accept: 144    Submit: 663

Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Whenever rxw meets Coral, he requires her to give him the laboratory key. Coral does not want to give him the key, so Coral ask him one question. if rxw can solve the problem, she will give him the key, otherwise
do not give him. rxw turns to you for help now,can you help him?
N children form a circle, numbered 0,1,2, ... ..., n-1,with Clockwise. Initially the ith child has Ai apples. Each round game, the ith child will obtain ( L*A(i+n-1)%n+R*A(i+1)%n ) apples. After m rounds game, Coral would like to know
the number of apples each child has. Because the final figure may be very large, so output the number model M.

 Input

The first line of input is an integer T representing the number of test cases to follow. Each case consists of two lines of input: the first line contains five integers n,m,L,R and M . the second line contains
n integers A0, A1, A2 ... An-1. (0 <= Ai <= 1000,3 <= n <= 100,0 <= L, R <= 1000,1 <= M <= 10 ^ 6,0 <=m < = 10 ^ 9). After m rounds game, output the number model M of apples each child has.

 Output

Each case separated by a space. See sample.

 Sample Input

1 3 2 3 4 10000 1 2 3

 Sample Output

120 133 131

 Source

FOJ月赛-2009年3月--- Coral

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

#define eps 1e-8
#define pi acos(-1.0)

#define LL __int64

using namespace std;

const int maxn = 110;

LL a[maxn], b[maxn], f[maxn];

LL mod, n;

void mul(LL a[], LL b[])
{
    LL c[maxn];
    memset(c, 0, sizeof(c));
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) c[i] = (a[j]*b[(i-j+n)%n]+c[i])%mod;
    memcpy(a, c, sizeof(c));
}

void pow_mod(LL a[], LL b)
{
    LL c[maxn];
    memset(c, 0, sizeof(c));
    c[0] = 1LL;
    while(b)
    {
        if(b&1) mul(c, a);
        mul(a, a);
        b >>= 1;
    }
    memcpy(a, c, sizeof(c));
}

int main()
{
    int T;
    cin>>T;
    LL m, l, r;
    while(T--)
    {
        cin>>n>>m>>l>>r>>mod;
        for(int i = 0;i < n;i++)  cin>>a[i];
        memset(f, 0, sizeof(f));
        f[0] = 1; f[1] = r; f[n-1]=l;
        pow_mod(f, m);
        LL ans[maxn];
        for(int i = 0; i < n; i++)
        {
            ans[i] = 0;
            for(int j = 0;j < n;j++)
                ans[i] = (ans[i]+a[j]*f[(i-j+n)%n])%mod;
        }
        cout<<ans[0];
        for(int i = 1; i < n; i++) cout<<" "<<ans[i];
        cout<<endl;
    }
    return 0;
}
时间: 2024-10-29 10:45:47

FZU Problem 1692 Key problem(循环矩阵)的相关文章

FZU - 1692 Key problem 矩阵快速幂

题目大意:有n个人围成一圈玩游戏,游戏有m个回合.每个人每回合能得到(L * 左边那个人的苹果数量) + (R * 右边那个人的苹果数量),问最后每个人得到的苹果数量 解题思路:矩阵的话比较好写,就不解释了,关键是怎么将矩阵乘法的复杂度下降,因为每个人每回合矩阵变化都是相同的,所以只需要求出第一行就可以了,经过偏移就可以得到其他行的了 这里还有一个剪枝点,传参时要用引用,不然会超时... #include<cstdio> #include<cstring> typedef long

hdu 4975 A simple Gaussian elimination problem.(网络流,判断矩阵是否存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and col

fzu 1753 Another Easy Problem

本题题意为求 t (t<150) 个 c (n,m)  (1<=m<=n<=100000)的最大公因子: 本题的难点为优化.主要有两个优化重点.一是每次对单个素因子进行处理,优化每次的数组清零:二是对求阶乘素因子个数的优化 ei=[N/pi^1]+ [N/pi^2]+ …… + [N/pi^n]  其中[]为取整 ei 为数 N!中pi 因子的个数: #include <iostream>#include <cstring>#include <cmat

贪心 FZU 2013 A short problem

题目传送门 1 /* 2 题意:取长度不小于m的序列使得和最大 3 贪心:先来一个前缀和,只要长度不小于m,从m开始,更新起点k最小值和ans最大值 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 1e6 + 10; 10 const int INF = 0x3f3f3f3f; 11 int a[MAXN], sum[MAXN]; 12 13 in

POJ3150—Cellular Automaton(循环矩阵)

题目链接:http://poj.org/problem?id=3150 题目意思:有n个数围成一个环,现在有一种变换,将所有距离第i(1<=i<=n)个数小于等于d的数加起来,对m取余,现在要求将所有的数都变换k次,得到的n个数的值. 思路:构造一个循环矩阵,以下这个矩阵是以样例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 我们发现n尽然达到了500,复杂度是n^3logk,过不了,我们发现这个矩阵长得很奇葩,每一行都是

hihocoder 1388 fft循环矩阵

#1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device which can send a particular 1 second signal repeatedly. The signal is A0 ... An-1 under n Hz sampling. One day, the device fell

UVA 1386 - Cellular Automaton(循环矩阵)

UVA 1386 - Cellular Automaton 题目链接 题意:给定一个n格的环,现在有个距离d,每次变化把环和他周围距离d以内的格子相加,结果mod m,问经过k次变换之后,环上的各个数字 思路:矩阵很好想,每个位置对应周围几个位置为1,其余位置为0,但是这个矩阵有500,有点大,直接n^3去求矩阵不太合适,然后观察发现这个矩阵是个循环矩阵,循环矩阵相乘的话,只需要保存一行即可,然后用n^2的时间就足够计算了 代码: #include <stdio.h> #include <

弱题(循环矩阵1)

问题 D: 弱题 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 28[提交][状态][讨论版] 题目描述 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M),若这个球标号为k(k < N),则将它重新标号为k + 1:若这个球标号为N,则将其重标号为1.(取出球后并不将其丢弃) 现在你需要求出,经过K次这样的操作后,每个标号的球的期望个数. 输入 第1

ubuntu:solve the problem of &#39;E:Problem with MergeList /var/lib/apt/lists/&#39;

just run this command: sudo rm /var/lib/apt/lists/* -vfR it will remove all the software package with the state of 'apt-get install' and no use to leave them, that's ok to just r.m. other conditons can refer to this article:http://blog.csdn.net/gopai