fuzhou 1692 Key problem ***

Problem 1692 Key problem

Accept: 103    Submit: 553 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
 
矩阵构造,题意有问题。R,L正好反了。
这一题的矩阵,第一次快速幂超时了,很无奈。原因在于Multiply()是f(n^3),   时间复杂度logm*n^3,
优化的地方,就是在这。
由于构造的矩阵很有规律是同构的。“什么是同构”?
如这一题:
123
312
231
这样的话只要求出第一排,那么其他就很好求了。n^2解决。不会超630ms
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 using namespace std;
  6 #define N 102
  7 typedef __int64 LL;
  8 LL n,m,L,R,mod;
  9 LL f[102];
 10 
 11 struct Matrix
 12 {
 13     LL mat[N][N];
 14     void ini()
 15     {
 16         memset(mat,0,sizeof(mat));
 17     }
 18     void init()
 19     {
 20         for(LL i=0;i<n;i++)
 21         for(LL j=0;j<n;j++)
 22         if(i==j) mat[i][j]=1;
 23         else mat[i][j]=0;
 24     }
 25     void make_first()
 26     {
 27         for(LL i=0;i<n;i++)
 28         {
 29            LL k1=(i+n-1)%n;
 30            mat[i][k1]=R;
 31            LL k2=(i+1)%n;
 32            mat[i][k2]=L;
 33         }
 34     }
 35 }M_hxl,M_tom;
 36 
 37 
 38 Matrix Multiply(Matrix &x, Matrix &y)
 39 {
 40     LL i, j, k;
 41     Matrix z;
 42     z.ini();
 43     for(k = 0; k < n; k ++)
 44         if(x.mat[0][k])
 45         {
 46             for(j = 0; j < n; j ++)
 47                 if(y.mat[k][j])
 48             z.mat[0][j] = (z.mat[0][j] + (LL)x.mat[0][k] * y.mat[k][j]) % mod;
 49         }
 50     for(i = 1; i < n; i ++)
 51     {
 52         z.mat[i][0] = z.mat[i - 1][n - 1];
 53         for(j = 1; j < n; j ++)
 54             z.mat[i][j] = z.mat[i - 1][j - 1];
 55     }
 56     return z;
 57 }
 58 void cs()
 59 {
 60     LL i,j;
 61     for(i=0;i<n;i++)
 62     {
 63         printf("
");
 64         for(j=0;j<n;j++)
 65         printf("%I64d ",M_hxl.mat[i][j]);
 66     }
 67     printf("
");
 68 }
 69 
 70 void power_sum2()
 71 {
 72     M_hxl.init();
 73     M_hxl.make_first();
 74     M_tom.init();
 75     cs();
 76     while(m)
 77     {
 78         if(m&1)
 79         {
 80             M_tom=Multiply(M_tom,M_hxl);
 81         }
 82         m=m>>1;
 83         M_hxl=Multiply(M_hxl,M_hxl);
 84     }
 85     for(LL i=0;i<n;i++)
 86     {
 87         LL sum=0;
 88         for(LL j=0;j<n;j++)
 89         {
 90             sum=(sum+f[j]*M_tom.mat[i][j])%mod;
 91         }
 92         if(i==0)printf("%I64d",sum);
 93         else printf(" %I64d",sum);
 94     }
 95     printf("
");
 96 }
 97 
 98 int main()
 99 {
100     LL T;
101    while(scanf("%I64d",&T)>0)
102     {
103         while(T--)
104         {
105             scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&L,&R,&mod);
106             for(LL i=0;i<n;i++)
107             scanf("%I64d",&f[i]);
108             if(m==0)
109             {
110                 for(LL i=0;i<n;i++)
111                 {
112                     if(i==0)printf("%I64d",f[i]%mod);
113                     else printf(" %I64d",f[i]%mod);
114                 }
115                 printf("
");
116                 continue;
117             }
118             power_sum2();
119         }
120     }
121     return 0;
122 }
 
 
 
 
原文地址:https://www.cnblogs.com/tom987690183/p/3293238.html