矩阵高速幂:

依据关系够建矩阵 , 高速幂解决.

Arc of Dream

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 2164    Accepted Submission(s): 680

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
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; typedef long long int LL; const LL mod=1000000007LL; struct Matrix
{
int x,y;
LL m[6][6];
Matrix() {x=y=5;memset(m,0,sizeof(m));}
void one()
{
for(int i=0;i<5;i++) m[i][i]=1LL;
}
void show()
{
cout<<x<<" * "<<y<<endl;
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
cout<<m[i][j]<<",";
cout<<endl;
}
}
}; Matrix Mul(Matrix& a,Matrix& b)
{
Matrix ret;
ret.x=a.x; ret.y=b.y;
for(int i=0;i<a.x;i++)
{
for(int j=0;j<b.y;j++)
{
LL temp=0;
for(int k=0;k<b.y;k++)
{
temp=(temp+(a.m[i][k]*b.m[k][j])%mod)%mod;
}
ret.m[i][j]=temp%mod;
}
}
return ret;
} Matrix quickPow(Matrix m,LL x)
{
Matrix e;
e.one();
while(x)
{
if(x&1LL) e=Mul(e,m);
m=Mul(m,m);
x/=2LL;
}
return e;
} LL n,A0,B0,AX,AY,BX,BY; Matrix init_matrix()
{
Matrix ret;
ret.m[0][0]=1;
ret.m[1][0]=AY; ret.m[1][1]=AX;
ret.m[2][0]=BY; ret.m[2][2]=BX;
ret.m[3][0]=(BY*AY)%mod; ret.m[3][1]=(AX*BY)%mod;
ret.m[3][2]=(BX*AY)%mod; ret.m[3][3]=(AX*BX)%mod;
ret.m[4][3]=1LL; ret.m[4][4]=1LL;
return ret;
} Matrix Beg()
{
Matrix beg;
beg.m[0][0]=1;
beg.m[1][0]=A0;
beg.m[2][0]=B0;
beg.m[3][0]=A0*B0%mod;
return beg;
} int main()
{
while(cin>>n)
{
cin>>A0>>AX>>AY>>B0>>BX>>BY;
A0=A0%mod; AX=AX%mod; AY=AY%mod;
B0=B0%mod; BX=BX%mod; BY=BY%mod;
Matrix m=init_matrix();
m=quickPow(m,n);
Matrix beg=Beg();
LL ans=0;
for(int i=0;i<5;i++)
ans=(ans+beg.m[i][0]*m.m[4][i]%mod)%mod;
cout<<ans<<endl;
}
return 0;
}

HDOJ 4686 Arc of Dream 矩阵高速幂的更多相关文章

  1. hdu 4686 Arc of Dream(矩阵快速幂)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...

  2. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

  3. HDU4686 Arc of Dream 矩阵快速幂

    Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  4. S - Arc of Dream 矩阵快速幂

    An Arc of Dream is a curve defined by following function: where a 0 = A0 a i = a i-1*AX+AY b 0 = B0  ...

  5. hdu----(4686)Arc of Dream(矩阵快速幂)

    Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  6. HDU4686——Arc of Dream矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4686 题目大意: 已知a0=A0, ai=Ax*ai-1+Ay; b0=B0, bi=Bx*bi-1 ...

  7. HDU 4686 Arc of Dream(矩阵)

    Arc of Dream [题目链接]Arc of Dream [题目类型]矩阵 &题解: 这题你做的复杂与否很大取决于你建的矩阵是什么样的,膜一发kuangbin大神的矩阵: 还有几个坑点: ...

  8. HDU4686 Arc of Dream —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4686 Arc of Dream Time Limit: 2000/2000 MS (Java/Others)    Memo ...

  9. HDOJ 5411 CRB and Puzzle 矩阵高速幂

    直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. redis的搜索组件 redis-search4j

    redis-search4j是一款基于redis的搜索组件. 特点 1.基于redis,性能高效 2.实时更新索引 3.支持Suggest前缀.拼音查找(AutoComplete功能) 4.支持单个或 ...

  2. ubuntu 13.04 编译 安装 升级 gcc 4.9.0 address sanitizer

    @ 前记: 最近查一个线上项目的crash,review代码无果,crash几率低,不可在本地环境重现.之后在线上好几个服务器跑valgrind就不crash了.个人猜测可能是跑valgrind后性能 ...

  3. Spring源码解析 – @Configuration配置类及注解Bean的解析

    在分析Spring 容器创建过程时,我们知道容器默认会加载一些后置处理器PostPRocessor,以AnnotationConfigApplicationContext为例,在构造函数中初始化rea ...

  4. powerdesigner反向SQLServer2008数据库生成物理数据模型

    方法一:通过数据库脚本生成物理数据模型 具体步骤如下图所示:

  5. 使用jquery加载部分视图02-使用$.ajax()

    本篇体验使用$.ajax()加载部分视图.与加载部分视图相关的包括: RenderPartial和RenderAction区别   使用jquery加载部分视图01-使用$.get()       □ ...

  6. spring mvc 下 applicationContext 和webApplicationContext

    spring中的ApplicationContexts可以被限制在不同的作用域.在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了Di ...

  7. 压缩 js/css 的工具

    最近检测服务器,发现js/css文件都没有压缩过,动手解决此问题先. 本次压缩采用 yui compress (2.4.8) 压缩脚本: #!/bin/sh echo "########## ...

  8. DTO vs. Assembly(转载)

    DTO vs. Assembly We probably need to make a strong statement about data transfer objects. Do we like ...

  9. 在JTextField中监听回车键,并执行相应按钮

    在jbInit() 中加入jTextField1的按键监听,代码如下:    jTextField1.addKeyListener(new KeyAdapter(){       public voi ...

  10. pandas过滤包含特定字符串的行

    ~df.col3.str.contains('u|z')也就是在条件前面加~号,表示not