Problem Description

The i’th Fibonacci number f(i) is recursively defined in the following way:

•f(0) = 0 and f(1) = 1
•f(i + 2) = f(i + 1) + f(i) for every i ≥ 0

Your task is to compute some values of this sequence

Input

Input begins with an integer t ≤ 10, 000, the number of test cases.

Each test case consists of three integers a, b, n where 0 ≤ a, b < 2 64 (a and b will not both be zero) and 1 ≤ n ≤ 1000.

Output

For each test case, output a single line containing the remainder of ƒ(ab ) upon division by n.

Sample Input

3

1  2
2 3 
744073709 184467955 

Sample Output


21

题目大意

给出a,b,n,让你计算f(a^b)%n,f(n)=f(n-1)+f(n-2);

因为是%n所以余数最多n*n种,于是我们就可以用快速幂求出是在数列中是第几个数,然后代入f[]

输出就可以了~

操作代码如下:(注:n&1为真则n为奇数)

#include<iostream>
#include<cstdio>
using namespace std;
#define ll unsigned long long
const int maxx=;
int f[maxx*maxx];
int pow(ll m,ll n,int k)
{
int b=;
while(n>)
{
if(n&)
{
b=(b*m)%k;
}
n=n>>;
m=(m*m)%k;
}
return b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll a,b;
int n,m;
scanf("%llu%llu%d",&a,&b,&n);
if(n==||a==)
printf("0\n");
else
{
f[]=;
f[]=;
m=n*n+;
int s;
for(int i=; i<=m; i++)
{
f[i]=(f[i-]+f[i-])%n;
if(f[i]==f[]&&f[i-]==f[])
{
s=i-;
break;
}
}
int k=pow(a%s,b,s);
printf("%d\n",f[k]);
}
}
return ;
}

最新文章

  1. 浅谈Apache性能调优
  2. Popup window
  3. 初识cocos2d-x-从环境配置到整体框架
  4. C# 实现关闭按钮隐藏窗体而不退出
  5. SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
  6. 证明 poj 1014 模优化修剪,部分递归 有错误
  7. Python学习笔记——进阶篇【第九周】———MYSQL操作
  8. Ubuntu cron定时执行任务
  9. hibernate一级缓存及对象的状态
  10. Mybatis第一天
  11. Asp.Net项目的部署到Linux中(Linux + Jexus+Nginx )
  12. 002.MySQL高可用主从复制部署
  13. Hash值破解工具(findmyhash与hash-identifier破解Hash值)
  14. 20181017 PL/SQL 记录
  15. hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
  16. python记录_day15 面向对象初识
  17. SQL注入之Sqli-labs系列第一关
  18. springboot jpa sql查询与传值
  19. vue中使用全局函数
  20. Jupyter notebook 使用多个Conda 环境

热门文章

  1. VMWare Fusion 8 序列号
  2. 1.1 OC class new summary
  3. Linux设备驱动程序 之 内存池
  4. AngularJS开发中常用的写法,如:获取URL参数、路由跳转、$http、获取元素等
  5. tp5 回滚事务记录,其中一条语句报错,全部回滚
  6. fastadmin 增加外键表搜索
  7. 解决阿里云轻量级服务器mysql无法用数据库操作软件连接
  8. IOS项目集成ShareSDK实现第三方登录、分享、关注等功能
  9. [Scikit-learn] 2.5 Dimensionality reduction - Probabilistic PCA &amp; Factor Analysis
  10. uni-app 使用 iconfont 图标 自定义图标