矩阵高速幂:

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

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. Tomcat启动异常 java.net.BindException: Cannot assign requested address: JVM_Bind

    从Apache官网下载的tomcat7,在MyEclipse中启动时抛出如下异常: 严重: StandardServer.await: create[localhost:8005]: java.net ...

  2. PostgreSQL各命令行工具功能说明

    I. SQL 命令 II. PostgreSQL 客户端应用 clusterdb -- 聚簇一个PostgreSQL数据库 createdb -- 创建一个新的PostgreSQL数据库 create ...

  3. .Net 中DataTable和 DataRow的 区别与联系

    1.简要说明二者关系 DataRow 和 DataColumn 对象是 DataTable 的主要组件.使用 DataRow 对象及其属性和方法检索.评估.插入.删除和更新 DataTable 中的值 ...

  4. oracle 取整的几种方法

    --1.取整(大)      select ceil(-1.001) value from dual ; --2.取整(小) select floor(-1.001) value from dual ...

  5. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

  6. Html.BeginForm() vs Ajax.BeginForm() in MVC3

    我们知道,BeginForm()方法能创建一个Form标签,因此可以结合表单级的方法,在这个页面中.我一直在考虑Html.BeginForm()方法和Ajax.BeginForm()方法在MVC3中有 ...

  7. C++ 模板的编译 以及 类模板内部的实例化

    在C++中.编译器在看到模板的定义的时候.并不马上产生代码,仅仅有在看到用到模板时,比方调用了模板函数 或者 定义了类模板的 对象的时候.编译器才产生特定类型的代码. 一般而言,在调用函数的时候,仅仅 ...

  8. MVC实现多选下拉框,保存并显示多选项

    在"MVC实现多选下拉框"中,主要是多选下拉框的显示,而实际情况通常是:选择多个选项提交后,需要在编辑页把所有选中的项显示出来. 模拟这样的一个场景:一个车迷可能有多个自己喜欢的汽 ...

  9. 报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"

    报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState".   出错语句停留 ...

  10. 使用jdbc的缺点

    使用jdbc开发时有以下缺点 1,数据库连接,使用时就创建,不使用就释放,对数据库进行频繁连接开关和关闭,造成数据库资源浪费,影响数据库的性能 解决:使用数据库连接池管理数据库的连接 2,sql语句使 ...