Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)
2024-10-06 17:13:05
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 ;
}
最新文章
- 浅谈Apache性能调优
- Popup window
- 初识cocos2d-x-从环境配置到整体框架
- C# 实现关闭按钮隐藏窗体而不退出
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- 证明 poj 1014 模优化修剪,部分递归 有错误
- Python学习笔记——进阶篇【第九周】———MYSQL操作
- Ubuntu cron定时执行任务
- hibernate一级缓存及对象的状态
- Mybatis第一天
- Asp.Net项目的部署到Linux中(Linux + Jexus+Nginx )
- 002.MySQL高可用主从复制部署
- Hash值破解工具(findmyhash与hash-identifier破解Hash值)
- 20181017 PL/SQL 记录
- hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
- python记录_day15 面向对象初识
- SQL注入之Sqli-labs系列第一关
- springboot jpa sql查询与传值
- vue中使用全局函数
- Jupyter notebook 使用多个Conda 环境
热门文章
- VMWare Fusion 8 序列号
- 1.1 OC class new summary
- Linux设备驱动程序 之 内存池
- AngularJS开发中常用的写法,如:获取URL参数、路由跳转、$http、获取元素等
- tp5 回滚事务记录,其中一条语句报错,全部回滚
- fastadmin 增加外键表搜索
- 解决阿里云轻量级服务器mysql无法用数据库操作软件连接
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能
- [Scikit-learn] 2.5 Dimensionality reduction - Probabilistic PCA &; Factor Analysis
- uni-app 使用 iconfont 图标 自定义图标