[Simulated game] to improve the group 19.8.5 A test Jizhong

Task 1. Matrix Games


Title effect: a n row m column matrix, the first row is number 1 2 3 ... m-1 m , the second row m + 1 m + 2 m + 3 ... 2m-1 2m, sequentially analogy, the behavior of the i-th (i-1) m + 1 (i-1) m + 2 (i-1) m + 3 ... (i-1) m + m-1 (i-1) m + m . There are k secondary operation, the digital matrix and (mod 10 after each operation of the first row of this matrix or x all numbers on the x-th column multiplied by C, all operations required 9 +7).

Data range: 1 ≦ N, M≤10 . 6 , 1≤K≤10

Obviously, the order of operations is not important, so we put the operation of the row and column operations separately.

R & lt provided i is the i-th row by the number, S i is the i th column multiplied by the number of all R & lt i S i at the beginning are both 1.

One kind of violence 80 ideas:

All processing operations on the first row, all rows directly calculated without considering the results for the operation of the column, plus all columns and multiplied S I results -1, so for a position (x, y) in the first the results will be counted R & lt X times, the results of the second will be calculated in S Y -1 times. Such a position would be counted (R & lt X + S Y -1) times, or not affect operated only counted once (R & lt X = 1, S Y = 1), an operation affects either (R & lt X = C, S Y =. 1, or R & lt X =. 1, S Y = C), affect the operation of either two (R & lt X = C, S Y = D). Such contributions should be the point of intersection R & lt X * S Y , minus the actual direct contribution of multi-operator plus the front, up to the number of intersection K 2 th. Complexity of O (N + M K + 2 ).

Correct answer:

The complexity of the bottleneck 80 approach is that the enumeration of the intersection, whether there is a valid method for the enumeration or may not consider the intersection of the intersection of the practice?

By observing the matrix, we found that if the operation is not listed, each column and form a arithmetic sequence (imagine we beat flat the matrix). If this time coupled with the above operation, we can direct to an item that the series is directly modified.

Maintenance of the line and the prefix and the first item of tolerance, tolerance, and the first item that is both above arithmetic progression, each to enumerate. Complexity of O (N + M).

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 template<class T>void read(T &x){
 7     x=0; char c=getchar();
 8     while(c<'0'||'9'<c)c=getchar();
 9     while('0'<=c&&c<='9'){x=(x<<1)+(x<<3)+(c^48); c=getchar();}
10 }
11 typedef long long ll;
12 const int N=1000050;
13 const int M=1000000007;
14 ll mul(int x,int y){ return (1ll*x*y)%M;}
15 
16 int n,m,k;
17 int r[N],s[N];
18 int a,d,ans;
19 int main(){
20 //    freopen("game.in","r",stdin);
21 //    freopen("game.out","w",stdout);
22     read(n); read(m); read(k);
23     for(int i=max(n,m);i;i--)r[i]=s[i]=1;
24     char op[3]; int x,c;
25     for(int i=1;i<=k;i++){
26         scanf("%s",op);
27         read(x); read(c);
28         if(op[0]=='R') r[x]=mul(r[x],c);
29         else s[x]=mul(s[x],c);
30     }
31     for(int i=1;i<=n;i++){
32         a=(1ll*a+mul((1ll*(i-1)*m+1)%M,r[i]))%M;
33         d=(d+r[i])%M;
34     }
35     for(int i=1;i<=m;i++){
36         ans=(1ll*ans+mul(a,s[i]))%M;
37         a=(a+d)%M;
38     }
39     printf("%d\n",ans);
40     return 0;
41 }
~ Same ~

PS: 1LL taught by a man.

Guess you like

Origin www.cnblogs.com/opethrax/p/11303600.html