Bear in the Field(CF-385E)
Problem Description
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.
Example
Input
5 1 2 0 1 2
output
3 1
Input
1 1 1 -1 -1 2
Input
1 1
題意:給出 n、x、y、dx、dy、t 代表有一個 n*n 的圖,每個點上行有 x+y 個草莓,現在從 (sx,sy) 出發,速度為 (dx,dy),其中對于每個點,每過一秒就會多 1 個草莓,速度增加當前位置上的草莓數,問 t 秒后的位置
思路:
根據題意,有:
其中,
對于時間 t,則有:
由于存在取模運算,%n 時只會產生 0~n-1 的數,因此將位置坐標都向上、向左平移一下,這樣坐標范圍變成 0~n-1,但這樣會導致速度增加量減少了 2,因此需要加上 2
于是,有:
代入?
有:
于是,可以構造矩陣:
化簡得:
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=1000000007; const int N=10+5; //const int dx[]= {-1,1,0,0}; //const int dy[]= {0,0,-1,1}; using namespace std; struct Matrix{LL s[N][N]; }; Matrix e;//單位矩陣E Matrix x;//構造矩陣 LL mod; Matrix mul(Matrix A,Matrix B,LL n){//矩陣乘法,n代表A、B兩個矩陣是n階方陣Matrix temp;//臨時矩陣,存放A*B結果for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)temp.s[i][j]=0;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++)temp.s[i][j]=((temp.s[i][j]+A.s[i][k]*B.s[k][j])%mod+mod)%mod;return temp; } Matrix quickPower(Matrix a,LL b,LL n){//矩陣快速冪,求矩陣n階矩陣的b次冪Matrix ans=e;while(b){if(b&1)ans=mul(ans,a,n);//ans=e*aa=mul(a,a,n);//a=a*ab>>=1;}return ans; } LL n,sx,sy,dx,dy,t; LL y[N]; void init(){for(int i=1;i<=6;i++)//主對角線為1e.s[i][i]=1;x.s[1][1]=2;x.s[1][2]=1;x.s[1][3]=1;x.s[1][4]=0;x.s[1][5]=1;x.s[1][6]=2;x.s[2][1]=1;x.s[2][2]=2;x.s[2][3]=0;x.s[2][4]=1;x.s[2][5]=1;x.s[2][6]=2;x.s[3][1]=1;x.s[3][2]=1;x.s[3][3]=1;x.s[3][4]=0;x.s[3][5]=1;x.s[3][6]=2;x.s[4][1]=1;x.s[4][2]=1;x.s[4][3]=0;x.s[4][4]=1;x.s[4][5]=1;x.s[4][6]=2;x.s[5][1]=0;x.s[5][2]=0;x.s[5][3]=0;x.s[5][4]=0;x.s[5][5]=1;x.s[5][6]=1;x.s[6][1]=0;x.s[6][2]=0;x.s[6][3]=0;x.s[6][4]=0;x.s[6][5]=0;x.s[6][6]=1;y[1]=sx-1;y[2]=sy-1;y[3]=dx;y[4]=dy;y[5]=0;y[6]=1; } int main(){while(scanf("%lld%lld%lld%lld%lld%lld",&n,&sx,&sy,&dx,&dy,&t)!=EOF){if(t==0)printf("%lld %lld",sx,sy);else{mod=n;init();Matrix res=quickPower(x,t,6);LL ex=0,ey=0;for(int i=1;i<=6;i++)ex=((ex+res.s[1][i]*y[i])%mod+mod)%mod;for(int i=1;i<=6;i++)ey=((ey+res.s[2][i]*y[i])%mod+mod)%mod;printf("%lld %lld\n",ex+1,ey+1);}}return 0; }?
總結
以上是生活随笔為你收集整理的Bear in the Field(CF-385E)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Naughty Stone Piles(
- 下一篇: 图论 —— 生成树 —— 最小生成树 —