E. Bear in the Field

Codeforces Round #226 (Div. 2)

E. Bear in the Field
time limit per test
1 second
memory limit per test
256 megabyte
 

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: nsxsydxdyt(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的草莓地,每个位置的初始草莓量为横坐标和纵坐标的和,然后每过一秒增长一个草莓。然后给出熊的初始位置(sx,sy),以及移动的速度(dx,dy),每一秒发生的事:(1)速度增加k(k为该位置的草莓数);(2)熊的位置发生移动;(3)每个位置上草莓数+1

 

 

矩阵快速幂

代码如下:

#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define M 6
LL MOD;
using namespace std;
struct Matrix{
    LL matrix[M][M];
};
int n;//矩阵的阶数 
void init(Matrix &res)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
            res.matrix[i][j]=0;
        res.matrix[i][i]=1;
    }
}
Matrix multiplicative(Matrix a,Matrix b)
{
    Matrix res;
    memset(res.matrix,0,sizeof(res.matrix));
    for(int i = 0 ; i < n ; i++)
        for(int j = 0 ; j < n ; j++)
            for(int k = 0 ; k < n ; k++)
                res.matrix[i][j] = (res.matrix[i][j]+a.matrix[i][k]%MOD*b.matrix[k][j]%MOD+MOD)%MOD;
    return res;
}
Matrix pow(Matrix mx,LL m)
{
    Matrix res,base=mx;
    init(res); //初始为单位矩阵,即除主对角线都是1外,其他都是0
    while(m)
    {
        if(m&1)
            res=multiplicative(res,base);
        base=multiplicative(base,base);
        m>>=1;
    }
    return res;
}
int main()
{
    LL sx,sy,dx,dy;
    LL t;
    n=6;
    Matrix base={
        2,1,1,0,1,2,
        1,2,0,1,1,2,
        1,1,1,0,1,2,
        1,1,0,1,1,2,
        0,0,0,0,1,1,
        0,0,0,0,0,1
    };
    scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&MOD,&sx,&sy,&dx,&dy,&t);
    Matrix res=pow(base,t);
    LL x,y;
    sx--;sy--;
    dx=(dx%MOD+MOD)%MOD;
    dy=(dy%MOD+MOD)%MOD;
    x=(res.matrix[0][0]*sx%MOD+res.matrix[0][1]*sy%MOD+res.matrix[0][2]*dx%MOD+res.matrix[0][3]*dy%MOD+res.matrix[0][5]%MOD+MOD)%MOD;
    y=(res.matrix[1][0]*sx%MOD+res.matrix[1][1]*sy%MOD+res.matrix[1][2]*dx%MOD+res.matrix[1][3]*dy%MOD+res.matrix[1][5]%MOD+MOD)%MOD;
    printf("%I64d %I64d\n",x+1,y+1);
    return 0;    
} 
View Code

 

posted @ 2018-01-12 16:59  jadelemon  阅读(252)  评论(0编辑  收藏  举报