codefroce385E矩阵快速幂

状态变化  (x,y,dx,dy,i) 表示i时刻熊站在(x,y)处速度向量(dx,dy)下一个状态是 ( 2x+y+dx+i , x+2y+dy+i , x+y+dx , x+y+dy , i+1 )

为了方便可以把平面从(1,1)平移到(0,0)  这时速度需要+2 (因为速度每次+x+y  x和y都-1则速度都+2)矩阵对应常数的地方为2

转移矩阵:{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}

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=10+5,maxn=1<<10+5,inf=0x3f3f3f3f;

struct Node{
   ll row,col;
   ll a[N][N];
};
ll n;
Node mul(Node x,Node y)
{
    Node ans;
    ans.row=x.row,ans.col=y.col;
    memset(ans.a,0,sizeof ans.a);
    for(ll i=0;i<x.row;i++)
        for(ll j=0;j<x.col;j++)
            for(ll k=0;k<y.col;k++)
                ans.a[i][k]=(ans.a[i][k]+x.a[i][j]*y.a[j][k]+n)%n;
    return ans;
}
Node quick_mul(Node x,ll n)
{
    Node ans;
    ans.row=x.row,ans.col=x.col;
    memset(ans.a,0,sizeof ans.a);
    for(ll i=0;i<ans.col;i++)ans.a[i][i]=1;
    while(n){
        if(n&1)ans=mul(ans,x);
        x=mul(x,x);
        n>>=1;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    ll x,y,dx,dy,t;
    cin>>n>>x>>y>>dx>>dy>>t;
    x--,y--;
    Node A;
    A.row=6,A.col=6;
    A.a[0][0]=2,A.a[0][1]=1,A.a[0][2]=1,A.a[0][3]=0,A.a[0][4]=1,A.a[0][5]=2;
    A.a[1][0]=1,A.a[1][1]=2,A.a[1][2]=0,A.a[1][3]=1,A.a[1][4]=1,A.a[1][5]=2;
    A.a[2][0]=1,A.a[2][1]=1,A.a[2][2]=1,A.a[2][3]=0,A.a[2][4]=1,A.a[2][5]=2;
    A.a[3][0]=1,A.a[3][1]=1,A.a[3][2]=0,A.a[3][3]=1,A.a[3][4]=1,A.a[3][5]=2;
    A.a[4][0]=0,A.a[4][1]=0,A.a[4][2]=0,A.a[4][3]=0,A.a[4][4]=1,A.a[4][5]=1;
    A.a[5][0]=0,A.a[5][1]=0,A.a[5][2]=0,A.a[5][3]=0,A.a[5][4]=0,A.a[5][5]=1;
    A=quick_mul(A,t);
    Node B;
    B.row=6,B.col=1;
    B.a[0][0]=x,B.a[1][0]=y,B.a[2][0]=dx,B.a[3][0]=dy,B.a[4][0]=0,B.a[5][0]=1;
    B=mul(A,B);
    cout<<(B.a[0][0]+n)%n+1<<" "<<(B.a[1][0]+n)%n+1<<endl;
    return 0;
}

时间: 2024-10-07 05:39:37

codefroce385E矩阵快速幂的相关文章

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

矩阵快速幂 模板与简单讲解

模板 快速幂模板 1 void solve(matrix t,long long o) 2 { 3 matrix e; 4 5 memset(e.a,0,sizeof(e.a)); 6 7 for (int i = 0;i < d;i++) 8 e.a[i][i] = 1; 9 10 while (o) 11 { 12 if (o & 1) 13 { 14 e = mul(e,t); 15 } 16 17 o >>= 1; 18 19 t = mul(t,t); 20 } 21

233 Matrix 矩阵快速幂

In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333...

2017省夏令营Day7 【快速幂,筛法,矩阵快速幂,线段树】

题解:首先,我们可以得到一个规律:经过2次变换后,a和b的值都分别乘2了,所以只要用快速幂就能过啦,但是,要特判n为0的情况. 代码如下: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #define Mod 1000000007 5 using namespace std; 6 long long a,b,n,ans1,ans2; 7 long long power(long long x)