Bear in the Field 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.

Input

The first line of the input contains six space-separated integers: n, sx, sy, dx, dy, t (1 ≤ n ≤ 1e9; 1 ≤ sx, sy ≤ n;  - 100 ≤ dx, dy ≤ 100; 0 ≤ t ≤ 1e18).

Output

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

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

题意:

给你一个矩阵,矩阵上每个点都有一个权值,权值初始为横纵坐标之和,以后每一秒权值加 1 ,现在熊从(x,y)出发,速度为(dx, dy);每一秒发生下列事:

  1. 速度增加k, k为那个点的权值,
  2. 熊的位置开始移动
  3. 每个位置权值加1.

思路:

矩阵快速幂,
根据题意,我们需要把坐标变到0 - n-1,
sx[t] = sx[t-1] + dx[t-1] + sx[t-1] + sy[t-1] + t-1 + 2(因为坐标为0~n-1)

sy[t] = sy[t-1] + dy[t-1] + sx[t-1] + sy[t-1] + t-1 + 2

dx[t] = dx[t-1] + sx[t-1] + sy[t-1] + t-1 + 2

dy[t] = dy[t-1] + sx[t-1] + sy[t-1] + t-1 + 2

t = t-1 + 1

所以递推矩阵:

211100121100101000010100111110222211


代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;

typedef long long LL;
LL n;

struct mat{
    LL a[7][7];
    mat (){memset(a, 0, sizeof(a)); }
    mat operator *(const mat q){
        mat c;
        for(int i = 1; i <= 6; ++i)
            for(int  k = 1; k <= 6; ++k)
            if (a[i][k])
        for(int j = 1; j <= 6; ++j){
            c.a[i][j] += a[i][k] * q.a[k][j];
            c.a[i][j] = (c.a[i][j] % n + n) % n;
        }return c;
    }
};

mat qpow(mat x, LL n){
    mat ans;
    for(int i = 1; i <= 6; ++i)
        ans.a[i][i] = 1;
    while(n){
        if (n&1) ans = ans * x;
        x = x * x;
        n >>= 1;
    }return ans;
}


int main(){
    LL  sx, sy, dx, dy, t;
    scanf("%lld %lld %lld %lld %lld %lld", &n, &sx, &sy, &dx, &dy, &t);
    mat ans;//构造矩阵
    ans.a[1][2] = ans.a[1][3] = ans.a[2][1] = ans.a[2][4] = ans.a[3][1] = 1;
    ans.a[3][2] = ans.a[3][3] = ans.a[4][1] = ans.a[4][2] = ans.a[4][4] = ans.a[5][5] = 1;
    ans.a[4][5] = ans.a[5][6] = ans.a[6][6] = ans.a[3][5] = 1;
    ans.a[1][5] = ans.a[2][5] = 1;
    ans.a[1][1] = ans.a[2][2] = ans.a[1][6] = ans.a[2][6] = ans.a[3][6] = ans.a[4][6] = 2;
   ans = qpow(ans,t);
    mat c;
    c.a[1][1] = sx-1;
    c.a[2][1] = sy-1;
    c.a[3][1] = dx;
    c.a[4][1] = dy;
    c.a[5][1] = 0;
    c.a[6][1] = 1;
    ans = ans * c;
    printf("%lld %lld\n",ans.a[1][1] + 1, ans.a[2][1] + 1);
    return 0;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值