codeforces 385E Bear in the Field

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


题意:刚开始在x,y的坐标,水平和竖直方向的速度分别是dx,dy,在某一点的位置的速度可增加x+y,每过一秒的速度增加量+1,求t秒后的位置坐标,位置坐标的范围在1-n

dx=dx+x+y+t;

dy=dy+x+y+t;

x=x+dx=2x+dx+y+t;

y=y+dy=x+2y+dy+t;

所以可以构造矩阵

2 1 1 0 1 0                  x

1 2 0 1 1 0                  y

1 1 1 0 1 0        X       dx

1 1 0 1 1 0                  dy

0 0 0 0 1 1                  0

0 0 0 0 0 1                  1

dy,dx的取值可能<0,可以等效为dx+=n


#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,mi
#define rson rt<<1|1,mi+1,r
#define root 1,1,n
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
const int mod=1e9+7;
ll ans[7][7],a[7][7];
ll b[7];
int n;

void init()
{
    mem(ans,0);
    for1(i,6)ans[i][i]=1;
    mem(a,0);
    a[1][2]=a[1][3]=a[1][5]=a[2][1]=a[2][4]=a[2][5]=1;
    a[3][1]=a[3][2]=a[3][3]=a[3][5]=a[4][1]=a[4][2]=a[4][4]=a[4][5]=1;
    a[5][5]=a[5][6]=a[6][6]=1;
    a[1][1]=a[2][2]=2;
}

void multi(ll a[][7],ll b[][7])
{
    ll c[7][7];
    mem(c,0);
    for1(i,6)
        for1(j,6)
            for1(k,6)
                c[i][j]=(c[i][j]+a[i][k]*b[k][j]-1)%n+1;
    for1(i,6)
        for1(j,6)
            a[i][j]=c[i][j];
}

void quick(ll x)
{
    while(x)
    {
        if(x&1)multi(ans,a);
        x>>=1;
        multi(a,a);
    }
}

int main()
{
    while(~sf("%d",&n))
    {
        init();
        for1(i,5)
            sf("%I64d",&b[i]);
        quick(b[5]);
        b[5]=0,b[6]=1;
	b[3]=(b[3]%n+n)%n;
	b[4]=(b[4]%n+n)%n;
        ll x1=0,y1=0;
        for1(i,6)
        {
            x1=(x1+ans[1][i]*b[i]-1)%n+1;
            y1=(y1+ans[2][i]*b[i]-1)%n+1;
        }
        pf("%I64d %I64d\n",x1,y1);
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值