hdu 5667 sequence
2016-04-17 13:10
316 查看
Problem Description
Holion
August will eat every thing he has found.
Now
there are many foods,but he does not want to eat all of them at once,so he find a sequence.
fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwise
He
gives you 5 numbers n,a,b,c,p,and he will eat fn foods.But
there are only p foods,so you should tell him fn mod
p.
Input
The
first line has a number,T,means testcase.
Each
testcase has 5 numbers,including n,a,b,c,p in a line.
1≤T≤10,1≤n≤1018,1≤a,b,c≤109,p is
a prime number,and p≤109+7.
Output
Output
one number for each case,which is fn mod
p.
Sample Input
Sample Output
由递推式可知,fn是a的次幂,设gn=log a fn,则式子可写成gn=b+gn-1*c+gn-2;可用矩阵乘法
Holion
August will eat every thing he has found.
Now
there are many foods,but he does not want to eat all of them at once,so he find a sequence.
fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwise
He
gives you 5 numbers n,a,b,c,p,and he will eat fn foods.But
there are only p foods,so you should tell him fn mod
p.
Input
The
first line has a number,T,means testcase.
Each
testcase has 5 numbers,including n,a,b,c,p in a line.
1≤T≤10,1≤n≤1018,1≤a,b,c≤109,p is
a prime number,and p≤109+7.
Output
Output
one number for each case,which is fn mod
p.
Sample Input
1 5 3 3 3 233
Sample Output
190
由递推式可知,fn是a的次幂,设gn=log a fn,则式子可写成gn=b+gn-1*c+gn-2;可用矩阵乘法
// //|G(n) | |c 1 b| |G(n-1)| //|G(n-1)|= |1 0 0|* |G(n-2)| //|1 | |0 0 1| |1 | #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; ll p; struct node { ll a[3][3]; }; node mul(node &a,node &b) { node t; memset(t.a,0,sizeof(t.a)); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { for(int k=0;k<3;k++) { t.a[i][j]+=(a.a[i][k]*b.a[k][j])%(p-1); t.a[i][j]%=(p-1); } } } return t; } node pow(node a,ll n) { node t; memset(t.a,0,sizeof(t.a)); for(int i=0;i<3;i++) t.a[i][i]=1; while(n) { if(n&1) t=mul(t,a); a=mul(a,a); n=n/2; } return t; } ll fun(ll a,ll b,ll c) { ll r=a%c,k=1; while(b) { if(b&1) k=(k*r)%c; r=(r*r)%c; b=b/2; } return k; } int main() { ll t,n,a,b,c; cin>>t; while(t--) { cin>>n>>a>>b>>c>>p; if(n==1) { cout<<1<<endl;continue; } if(n==2) { cout<<fun(a,b,p)<<endl;continue; } node t; t.a[0][0]=c,t.a[0][1]=1,t.a[0][2]=b; t.a[1][0]=1,t.a[1][1]=0,t.a[1][2]=0; t.a[2][0]=0,t.a[2][1]=0,t.a[2][2]=1; node t1; t1=pow(t,n-2); ll ans=t1.a[0][0]*b+t1.a[0][2]; ans=fun(a,ans,p); cout<<ans<<endl; } }
相关文章推荐
- iOS UIScrollView和 cell加载子视图偏移64问题
- HDU 5667 Sequence
- ProgressBar 为什么可以在非UI线程中更新进度。
- 为什么说Druid是“最好的数据库连接池”?体现在哪些方面?这是如何实现的? (mybatis是不自带jdbc链接池的)
- Arduino中的数据类型
- django-1366, "Incorrect string value: '\\xE6\\x88\\x9A\\xE4\\xBC\\x9F...'
- django-1366, "Incorrect string value: '\\xE6\\x88\\x9A\\xE4\\xBC\\x9F...'
- 斐波那契数列 Fibonacci sequence
- 安装VBox共享文件增强功能VBoxGuestAdditions
- MySQL: table、view、trigger、procedure、index (表、主键、UNIQUE Key、列、索引、视图、触发器、存储过程)查看、修改、更新、删除操作
- 使用fuel6.0自动安装openstack-juno版本(2)
- iOS蓝牙4.0(BLE)-LightBlue与cc2540开发板通信
- HDU5667 sequence 递推求值 (矩阵快速幂)
- [HDU 5667] Sequence (矩阵快速幂+费马小定理)
- HDU 5667 Sequence 矩阵快速幂
- java 集合-ArrayDeque
- java.sql.SQLException: Value'0000-00-00'错误解决方法
- 太阳神三国杀源代码 HOW TO BUILD
- Zero Requiem
- HDU 5667 Sequence(矩阵快速幂)