题目链接:https://vjudge.net/problem/CodeForces-385E
Our bear's forest has a checkered field. The checkered field is an n × n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row x and column y by record (x, y). Each cell of the field contains growing raspberry, at that, the cell (x, y) of the field contains x + y raspberry bushes.
The bear came out to walk across the field. At the beginning of the walk his speed is (dx, dy). Then the bear spends exactly t seconds on the field. Each second the following takes place:
- Let's suppose that at the current moment the bear is in cell (x, y).
- First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from k bushes, he increases each component of his speed by k. In other words, if before eating the k bushes of raspberry his speed was (dx, dy), then after eating the berry his speed equals (dx + k, dy + k).
- Let's denote the current speed of the bear (dx, dy) (it was increased after the previous step). Then the bear moves from cell (x, y) to cell (((x + dx - 1) mod n) + 1, ((y + dy - 1) mod n) + 1).
- Then one additional raspberry bush grows in each cell of the field.
You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (sx, sy). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.
The first line of the input contains six space-separated integers: n, sx, sy, dx, dy, t(1 ≤ n ≤ 109; 1 ≤ sx, sy ≤ n; - 100 ≤ dx, dy ≤ 100; 0 ≤ t ≤ 1018).
Print two integers — the coordinates of the cell the bear will end up in after t seconds.
5 1 2 0 1 2
3 1
1 1 1 -1 -1 2
1 1
Operation a mod b means taking the remainder after dividing a by b. Note that the result of the operation is always non-negative. For example, ( - 1) mod 3 = 2.
In the first sample before the first move the speed vector will equal (3,4) and the bear will get to cell (4,1). Before the second move the speed vector will equal (9,10) and he bear will get to cell (3,1). Don't forget that at the second move, the number of berry bushes increased by 1.
In the second sample before the first move the speed vector will equal (1,1) and the bear will get to cell (1,1). Before the second move, the speed vector will equal (4,4) and the bear will get to cell (1,1). Don't forget that at the second move, the number of berry bushes increased by 1.
题解:
1.为了方便取模,把x、y轴都改成从0开始,最后加1即可。设(sx[t], sy[t])为t时刻的位置,(dx[t], dy[t])为从t-1到t时间段的速度(偏移量),根据题意,可得:
dx[t] = dx[t-1] + sx[t-1] +1 + sy[t-1]+1 + t-1
dy[t] = dy[t-1] + sx[t-1] +1 + sy[t-1]+1 + t-1
sx[t] = sx[t-1] + dx[t-1] + sx[t-1] +1 + sy[t-1]+1 + t-1
sy[t] = sy[t-1] + dy[t-1] + sx[t-1] +1 + sy[t-1]+1 + t-1
2.根据上述递推式,构造矩阵求解即可。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 //const int MOD = 1e9+7; 17 const int MAXN = 1e6+100; 18 19 int MOD; 20 const int Size = 6; 21 struct MA 22 { 23 LL mat[Size][Size]; 24 void init() 25 { 26 for(int i = 0; i<Size; i++) 27 for(int j = 0; j<Size; j++) 28 mat[i][j] = (i==j); 29 } 30 }; 31 32 MA mul(MA x, MA y) 33 { 34 MA ret; 35 memset(ret.mat, 0, sizeof(ret.mat)); 36 for(int i = 0; i<Size; i++) 37 for(int j = 0; j<Size; j++) 38 for(int k = 0; k<Size; k++) 39 ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j]%MOD+MOD)%MOD, ret.mat[i][j] %= MOD; 40 return ret; 41 } 42 43 MA qpow(MA x, LL y) 44 { 45 MA s; 46 s.init(); 47 while(y) 48 { 49 if(y&1) s = mul(s, x); 50 x = mul(x, x); 51 y >>= 1; 52 } 53 return s; 54 } 55 56 MA tmp = { 57 1,0,1,1,1,2, 58 0,1,1,1,1,2, 59 1,0,2,1,1,2, 60 0,1,1,2,1,2, 61 0,0,0,0,1,1, 62 0,0,0,0,0,1 63 }; 64 65 int main() 66 { 67 LL n, sx, sy, dx, dy, t; 68 while(scanf("%lld%lld%lld%lld%lld%lld",&n,&sx,&sy,&dx,&dy,&t)!=EOF) 69 { 70 MOD = n; 71 MA s = tmp; 72 s = qpow(s, t); 73 74 sx--; sy--; 75 LL a[6] = {dx,dy,sx,sy,0,1}; 76 sx = sy = 0; 77 for(int i = 0; i<Size; i++) 78 { 79 sx += (1LL*s.mat[2][i]*a[i]%MOD+MOD)%MOD, sx %= MOD; 80 sy += (1LL*s.mat[3][i]*a[i]%MOD+MOD)%MOD, sy %= MOD; 81 } 82 printf("%lld %lld\n", sx+1, sy+1); 83 } 84 }