• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FightingForWorldFinal
博客园    首页    新随笔    联系   管理    订阅  订阅

HDU4686矩阵快速幂

Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 297    Accepted Submission(s): 93


Problem Description
An Arc of Dream is a curve defined by following function:

where
a0 = A0
ai = ai-1*AX+AY
b0 = B0
bi = bi-1*BX+BY
What is the value of AoD(N) modulo 1,000,000,007?
 

 

Input
There are multiple test cases. Process to the End of File.
Each test case contains 7 nonnegative integers as follows:
N
A0 AX AY
B0 BX BY
N is no more than 1018, and all the other integers are no more than 2×109.
 

 

Output
For each test case, output AoD(N) modulo 1,000,000,007.
 

 

Sample Input
1
1 2 3
4 5 6
2
1 2 3
4 5 6
3
1 2 3
4 5 6
 
 

 

Sample Output
4 134 1902
 

 

Author
Zejun Wu (watashi)
 

 

Source
2013 Multi-University Training Contest 9
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define eps 1e-6
typedef long long LL;

const int N=5;//定义NxN矩阵 
#define mod 1000000007//取模 
struct matrix
{
    LL a[N][N];
}origin,res;
matrix multiply(matrix& x,matrix& y)//矩阵乘 
{
    matrix temp;
    memset(temp.a,0,sizeof(temp.a));
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            for(int k=0;k<N;k++)
            {
                temp.a[i][j]=(temp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;
            }
        }
    }
    return temp;
}
matrix MatrixPow(LL n)//矩阵快速幂,求n次方 
{
    while(n)
    {
        if(n&1)
            res=multiply(res,origin);
        n/=2;
        origin=multiply(origin,origin);
    }
    return res;
}
int main()
{
    #ifdef DeBUG
        freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin);
    #endif
    LL a0,ax,ay;
    LL b0,bx,by;
    LL n;
    while(scanf("%I64d",&n)+1)
    {
        LL i,j,k;
        memset(res.a,0,sizeof(res.a));
        memset(origin.a,0,sizeof(origin.a));
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a0,&ax,&ay,&b0,&bx,&by);
        res.a[0][1]=a0*b0%mod;  
        res.a[0][2]=a0%mod;  
        res.a[0][3]=b0%mod;  
        res.a[0][4]=1;  
        origin.a[0][0]=1;  
        origin.a[1][0]=1;
        origin.a[1][1]=(ax*bx)%mod;  
        origin.a[2][1]=ax*by%mod;
        origin.a[2][2]=ax%mod;  
        origin.a[3][1]=bx*ay%mod;
        origin.a[3][3]=bx%mod;  
        origin.a[4][1]=ay*by%mod;
        origin.a[4][2]=ay%mod;
        origin.a[4][3]=by%mod;
        origin.a[4][4]=1;  
        MatrixPow(n);  
        cout<<res.a[0][0]<<endl;  

    }
    return 0;
}
/* 
res 
 
AoD ai*bi ai bi 1 
0    0    0   0 0 
0    0    0   0 0 
0    0    0   0 0 
0    0    0   0 0 

org 

1   0   0   0   0 
1 ax*bx 0   0   0 
0 ax*by ax  0   0 
0 bx*ay 0   bx  0 
0 ay*by ay  by  1 
*/  
View Code

矩阵快速幂,可做模板用

没想到这题能这么搞,现在终于知道矩阵快速幂的实用价值了

话说还有公式版的,那个太繁琐就不搞了

posted @ 2013-08-20 21:04  Sky-J  阅读(328)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3