题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5667

题意:

Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到了一个数列:

fn=

1,ab,abfcn−1fn−2,n=1n=2otherwise

给定各个数,求fn。

分析:

可以发现最后都是a的倍数,这样我们让fn对a取对数,令tn=logafn方程就转化为b+ctn−1+tn−2,这样利用矩阵快速幂直接算幂数,最后快速幂一下就可以了。

注意:

  • 由费马小定理可知,ab%p=ab/(p−1)∗(p−1)+b%(p−1)%p=ab/(p−1)∗(p−1)%p∗ab%(p−1)%p=ab%(p−1)%p,所以矩阵快速幂的模应该为p−1。
  • 特别注意a%p==0的时候,答案应该为0。

代码:

#include<cstdio>
const int N = 105;
int mod = 1e9 + 7;
struct Matrix
{
int row,cal;
long long m[N][N];
};
Matrix init(Matrix a, long long t)
{
for(int i = 0; i < a.row; i++)
for(int j = 0; j < a.cal; j++)
a.m[i][j] = t;
return a;
}
Matrix mul(Matrix a,Matrix b)
{
Matrix ans;
ans.row = a.row, ans.cal = b.cal;
ans = init(ans,0);
for(int i = 0; i < a.row; i++)
for(int j = 0; j < b.cal; j++)
for(int k = 0; k < a.cal; k++)
ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j])%mod;
return ans;
}
int quickpow(int a, int b, int mod)
{
int ans = 1;
for(;b;b >>= 1, a = a * 1ll * a % mod){
if(b & 1) ans = ans * 1ll * a % mod;
}
return ans;
}
int quick_pow(long long k, int b, Matrix A)
{
if(k < 0) return 0;
if(k == 0) return b;
Matrix I;
I.row = 3, I.cal = 1;
I = init(I, 0);
I.m[0][0] = 1;
I.m[1][0] = b;
I.m[2][0] = 0;
while(k){
if(k & 1) I = mul(A, I);
A = mul(A, A);
k>>=1;
}
return I.m[1][0]%mod;
}
int main (void)
{
int T;scanf("%d", &T);
while(T--){
int a, b, c, p;
long long n;
scanf("%I64d%d%d%d%d", &n, &a,&b, &c, &p);
if(a % p == 0){printf("0\n");continue;}
mod = p - 1;
Matrix A;
A.row = 3, A.cal = 3;
A = init(A, 0);
A.m[0][0] = A.m[2][1] = A.m[1][2] = 1;
A.m[1][0] = b;A.m[1][1] = c;
int res = quick_pow(n - 2, b, A);
printf("%d\n", quickpow(a, res, p));
}
}

最新文章

  1. TestNG插件的安装问题
  2. poj 3070
  3. Ext.NET-基础篇
  4. android studio This client is too old to work with the working copy at
  5. AOP——代理技术
  6. html5人物图片360度立体旋转
  7. poj 2186 tarjan求强连通分量
  8. The include feature of SQL Server Index
  9. RHCE7.0练习题汇总[转]
  10. highstock实现股票分时
  11. linux防火墙之 ufw
  12. 《java入门第一季》之面向对象(static关键字)
  13. 【朝花夕拾】Android性能篇之(六)Android进程管理机制
  14. stm32输入的功能引脚功能介绍
  15. spring 自己创建配置类
  16. AM335X启动(转)
  17. .net获取程序根目录
  18. SoapUI&#160;利用SoapUI进行简单的接口并发测试
  19. HttpWatch手把手图解教程
  20. (转)使用FFMPEG类库分离出多媒体文件中的H.264码流

热门文章

  1. Bootstrap历练实例:禁用的按钮
  2. shell脚本,awk实现行列转换
  3. c++ 当输入的数据不符合数据类型时,清理输入流
  4. 【Java_多线程并发编程】基础篇——线程状态扭转函数
  5. 主DNS服务-正向解析
  6. centos7下添加开机启动
  7. python基础学习笔记——包
  8. 00048_this关键字
  9. A. Light Bulb
  10. 九度oj 题目1528:最长回文子串