UVa #11582 Colossal Fibonacci Numbers!
2016-04-25 11:46
363 查看
[b]巨大的斐波那契数[/b]
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 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 < 264 (a andb will not both be zero) and 1 ≤ n ≤ 1000.
For each test case, output a single line containing the remainder of f (ab) upon division by n.
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 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 < 264 (a andb will not both be zero) and 1 ≤ n ≤ 1000.
For each test case, output a single line containing the remainder of f (ab) upon division by n.
Sample input
3 1 1 2 2 3 1000 18446744073709551615 18446744073709551615 1000
Sample output
1 21 250 题意: 输 入两个非负整数a、b和正整数n(0<=a,b<=2^64,1<=n<=1000),让你计算f(a^b)对n取模的值, 其中f(0) = 0,f(1) = 1;且对任意非负整数i,f(i+2)= f(i+1)+f(i)。 分析: 因为斐波那契序列要对n取模,余数只有n种,所以最多n^2项序列就开始重复,所以问题转化成了求周期然后大整数取模。 小于2^64的数要用unsigned long long。
#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int maxn=1000+5; int m[maxn],n; typedef unsigned long long ULL; unsigned long long a,b; unsigned long long f[maxn][3100]; int pow_mod(ULL a,ULL b,int n) { if(b==0) return 1; int x=pow_mod(a,b/2,n); unsigned long long ans=(unsigned long long )x*x%n; if(b%2==1) ans=ans*a%n; return (int)ans; } int main() { for(n=2;n<=1000;n++) { f [0]=0,f [1]=1; for(int i=2;;i++) { f [i]=(f [i-1]+f [i-2])%n; if(f [i]==1&&f [i-1]==0) { m =i-1; break; } } } int t; scanf("%d",&t); while(t--) { scanf("%llu%llu%d",&a,&b,&n); if(n==1||a==0) printf("0\n"); else printf("%d\n",f [pow_mod(a%m ,b,m )]); } return 0; }
相关文章推荐
- 【机器学习算法】基于R语言的多元线性回归分析
- main函数——命令行参数与环境变量
- python re 模块
- 值班记录本
- 联想Lenove台式机进入BIOS,设置U盘为启动项+破解电脑密码(老毛桃+cmd)
- Java语法糖2:自动装箱和自动拆箱
- 鸟哥私房菜linux基础学习笔记 6
- android studio如何创建并依赖library module
- java中RSA加解密的实现
- JSP的执行过程及其异常处理机制
- ScrollView中嵌套ListView时显示不全的问题解决办法
- dll之路2:dll的作用介绍
- Python中的引用和拷贝浅析
- ASP.NET mvc 在视图中使用@helper封装输出代码
- 工具类-虚拟键盘相关大全
- 图片加载的学习体会吧
- 资源分配问题
- 【.net 深呼吸】自定义缓存配置(非Web项目)
- linux设置定时任务
- hp prodesk 498g2 mt 升级win10后网卡红叉叉,灯不闪烁