您的位置:首页 > 其它

解题报告:Codeforces Round #226 (Div. 2)E. Bear in the Field 矩阵加速幂

2016-08-21 19:50 513 查看
E. Bear in the Field

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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).

Output

Print two integers — the coordinates of the cell the bear will end up in after t seconds.

Examples

input
5 1 2 0 1 2


output
3 1


input
1 1 1 -1 -1 2


output
1 1


Note

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.

题意:

一只小熊在一块n*n的田野表格上吃草莓,每个格子上的初始草莓数目为行列和(如1,1处的初始草莓为2),每过一秒,所有格子上的草莓数目+1。

若当前小熊的位置为(x,y),小熊的两个方向上的速度为(dx,dy),当前格子上的草莓数为k,那么下一秒小熊的位置为(x+dx+k,y+dy+k),小熊的速度为(dx+k,dy+k)。注意:每个格子上的草莓数不会变小,也就是说第t秒时,(x,y)处的草莓数目为x+y+t。

现在给出小熊的初始位置x,y,初始速度,dx,dy,时间t,让你预测第t秒时小熊的位置。

思路:

只需要注意到每个时刻每个格子的草莓都是可以知道的,那么很容易想到递推式,然后用矩阵快速幂求解

dx[t+1] = dx[t] + x[t] + y[t]
+ t[t] 

dy[t+1] = dy[t] + x[t] + y[t]
+ t[t]

  x[t+1] = x[t] + dx[t+1] = dx[t] + 2 * x[t] + y[t]
+ t[t]

  y[t+1] = y[t] + dy[t+1] = dy[t] + x[t]
+ 2 * y[t] + t[t]

   t[t+1] = t[t] + 1

注意下求mod 。

矩阵为

long long mat[6][6] = {
1,0,1,1,1,0,
0,1,1,1,1,0,
1,0,2,1,1,0,
0,1,1,2,1,0,
0,0,0,0,1,1,
0,0,0,0,0,1
};


代码:

#include<bits/stdc++.h>

using namespace std;

long long n,t;

long long ans[6],X,Y;
long long&sx=ans[2],&sy=ans[3],&dx=ans[0],&dy=ans[1];

inline void cp(long long a[6][6],long long b[6][6]){
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
a[i][j] = b[i][j];
}
}
}

inline void debug(long long a[6][6]){
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
printf("%I64d%c",a[i][j],(j==5LL)?'\n':' ');
}
}printf("\n");
}

void fast_mat(){
ans[4] = 0;
ans[5] = 1;
long long mat[6][6] = { 1,0,1,1,1,0, 0,1,1,1,1,0, 1,0,2,1,1,0, 0,1,1,2,1,0, 0,0,0,0,1,1, 0,0,0,0,0,1 };
long long res[6][6] = {
1,0,0,0,0,0,
0,1,0,0,0,0,
0,0,1,0,0,0,
0,0,0,1,0,0,
0,0,0,0,1,0,
0,0,0,0,0,1
};
long long tmp[6][6];
while(t){
//debug(res);
if(t&1){
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
tmp[i][j] = 0 ;
for(int k=0;k<6;k++){
tmp[i][j] = (tmp[i][j] + res[k][j] * mat[i][k] % n) % n ;
}
}
}cp(res,tmp);
}
t >>= 1;
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){tmp[i][j] = 0;
for(int k=0;k<6;k++){
tmp[i][j] = (tmp[i][j] + mat[k][j] * mat[i][k] % n) % n ;
}
}
}cp(mat,tmp);
}//debug(res);
X = Y = 0;
//printf("ans[]:\n");
//for(int i=0;i<6;i++){
// printf("%I64d%c",ans[i],((i!=5)?' ':'\n'));
//}

for(int k=0;k<6;k++){
X = (X + res[2][k]*ans[k] % n)%n;
}X=(X+n)%n;if(!X)X+=n;
for(int k=0;k<6;k++){
Y = (Y + res[3][k]*ans[k] % n)%n;
}Y=(Y+n)%n;if(!Y)Y+=n;
}

int main()
{
while(scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&n,&sx,&sy,&dx,&dy,&t)==6){
fast_mat();
printf("%I64d %I64d\n",X,Y);
}return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: