Codeforces Round #226 (Div. 2) E---Bear in the Field(矩阵)

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.

Sample test(s)

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)

下标从0开始

dx = dx’ + x’ + y’ + 2 + t - 1

dy = dy’ + x’ + y’ + 2 + t - 1

x = x’ + dx

y = y’ + dy

t = t - 1 + 1

整理后:

x = 2 * x’ + y’ + dx’ + t + 1

y = 2 * y’ + x’ + dy’ + t + 1

设有列向量 X = {x, y, dx, dy, t, 1}, X’ = {x’, y’, dx’, dy’, t - 1, 1}

X = A * X’

显然转移矩阵A为

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

/*************************************************************************
    > File Name: CF226-E.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年03月19日 星期四 15时31分59秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int mod, sx, sy, dx, dy;
LL t;

class MARTIX
{
    public:
        LL mat[15][15];
    public:
        MARTIX();
        MARTIX(LL tmp[6][6]);
        MARTIX operator * (const MARTIX &b)const;
        MARTIX& operator = (const MARTIX &b);
};

MARTIX :: MARTIX()
{
    memset (mat, 0, sizeof(mat));
}

MARTIX :: MARTIX(LL tmp[6][6])
{
    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            mat[i][j] = tmp[i][j];
        }
    }
}

MARTIX MARTIX :: operator * (const MARTIX &b)const
{
    MARTIX ret;
    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            for (int k = 0; k < 6; ++k)
            {
                ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];
                ret.mat[i][j] %= mod;
            }
        }
    }
    return ret;
}

MARTIX& MARTIX :: operator = (const MARTIX &b)
{
    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            this -> mat[i][j] = b.mat[i][j];
        }
    }
    return *this;
}

void Debug(MARTIX A)
{
    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 6; ++j)
        {
            printf("%lld ", A.mat[i][j]);
        }
        printf("\n");
    }
}

MARTIX fastpow(MARTIX A, LL cnt)
{
    MARTIX ans;
    for (int i = 0; i < 6; ++i)
    {
        ans.mat[i][i] = 1;
    }
    while (cnt)
    {
        if (cnt & 1)
        {
            ans = ans * A;
        }
        cnt >>= 1;
        A = A * A;
    }
    return ans;
}    

int main()
{
    LL tmp[6][6] = {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};
    MARTIX A(tmp);
//  Debug(A);
    while (~scanf("%d%d%d%d%d%lld", &mod, &sx, &sy, &dx, &dy, &t))
    {
        --sx;
        --sy;
        MARTIX X = fastpow(A, t);
        MARTIX F;
        F.mat[0][0] = sx;
        F.mat[1][0] = sy;
        F.mat[2][0] = (dx % mod + mod) % mod;
        F.mat[3][0] = (dy % mod + mod) % mod;
        F.mat[5][0] = 1;
        F = X * F;
        printf("%lld %lld\n", F.mat[0][0] + 1,  F.mat[1][0] + 1);
    }
    return 0;
}
时间: 2024-10-18 10:46:33

Codeforces Round #226 (Div. 2) E---Bear in the Field(矩阵)的相关文章

Codeforces Round #226 (Div. 2)--A Bear and Raspberry

题目链接:Bear and Raspberry Bear and Raspberry time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The bear decided to store some raspberry for the winter. He cunningly found out the price for a ba

【计算几何】【状压dp】Codeforces Round #226 (Div. 2) D. Bear and Floodlight

读懂题意发现是傻逼状压. 只要会向量旋转,以及直线求交点坐标就行了.(验证了我这俩板子都没毛病) 细节蛮多. #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const double PI=acos(-1.0); #define EPS 0.00000001 struct Point { double x,y; Point(

Codeforces Round #226 (Div. 2) C. Bear and Prime Numbers(暴力)

题目链接:点击打开链接 题意:给n个数, m次询问, 每次询问时一个区间[l, r]. 求这个区间中的所有素数,能被n个数中的几个数整除的累加和. 思路:没想到什么好办法, 直接筛出素数以后直接暴力的,没想到跑的这么快.. 最费时间的大概就是那个二重循环, 但是可能因为素数比较少, 所以实际的时间复杂度并不高. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostr

Codeforces Round #226 (Div. 2):Problem 385C - Bear and Prime Numbers (素数刷法+前缀和)

Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeForces. Original ID: 385C 64-bit integer IO format: %I64d      Java class name: (Any) Prev Submit Status Statistics Discuss Next Type: None Recently, the bear started studyi

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces Round #226 (Div. 2) C

题目: CF机子真心强大啊,这样才跑了600ms,给了你n个数的序列,然后m次询问,每次询问求出序列中每个数是 区间[a,b]内的 几个素数的倍数统计一下,然后对于个数求和,看了题目下面的hint很易懂,然后看到a,b的范围有些大哈,2*10^9,不知道怎么处理,但是后来发现,序列中的数 最大为10^7,所以就算a,b,再大也无所谓的,大于序列中的最大数的部分的素数,序列中不会有任何数 是它倍数的,然后就是对10^7以内的 素数进行预处理,同时把序列中的数统计一下个数,在预处理素数的同时 会有一

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

不要想多了直接暴.对于u枚举a和b,判断一个是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; const int maxn = 4001; #define PB push_back vector<int> G[maxn]; bool g[maxn][maxn]; int deg[maxn]; const int INF = 0x3f3f3f3f; int main() { //freopen("

Codeforces Round #318 (Div. 2) A Bear and Elections

优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main() { int n; scanf("%d",&n); int t; scanf("%d",&t); for(int i = 2; i <= n; i++){ int v; scanf("%d",&v); q.push(v

Codeforces Round #318 (Div. 2) C Bear and Poker

很简单,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll a[maxn]; int main() { //freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(i