快速幂取模+数学知识(Colossal Fibonacci Numbers! uva 11582)
来源:互联网 发布:免费代理记账软件 编辑:程序博客网 时间:2024/06/06 00:06
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
31 1 22 3 100018446744073709551615 18446744073709551615 1000
Sample output
121250
翻译一下,就是告诉你a,b,n,让你计算f(a^b)%n,f(n)=f(n-1)+f(n-2);
因为是%n所以余数最多n*n种,于是我们就可以用快速幂求出是在数列中是第几个数,然后代入f[]输出就可以了~
#include<bits/stdc++.h>#define LL unsigned long longusing namespace std;vector<int> f[1005];int num[1005];LL Pow(LL a,LL b,LL m){if(b==0) return 1;LL ans=Pow(a,b/2,m)%m;if(b%2==0) return (ans*ans)%m;else return (ans*ans*a)%m;}int init()//初始化减少重复运算,用vector数组储存空间比较小 {for(int i=2;i<=1000;i++){f[i].push_back(0);f[i].push_back(1);for(int j=2;;j++){f[i].push_back((f[i][j-1]+f[i][j-2])%i);if(f[i][j]==1&&f[i][j-1]==0)//如果相同了{num[i]=j-1;break;} }}}int main(){LL t,a,b,n;ios::sync_with_stdio(false);init();cin>>t;while(t--){cin>>a>>b>>n;if(a==0||n==1) cout<<"0\n";else{cout<<f[n][Pow(a%num[n],b,num[n])]<<endl;//在num[n]的时候表示出现重复了 }}}
第一次写uva的题目感觉有点吃力,老外的题库就是这样子~
阅读全文
0 0
- 快速幂取模+数学知识(Colossal Fibonacci Numbers! uva 11582)
- uva 11582 - Colossal Fibonacci Numbers!(整数快速幂)
- 【UVA】11582 - Colossal Fibonacci Numbers!(快速幂)
- UVA 11582 Colossal Fibonacci Numbers!(打表+快速幂)
- UVA - 11582 - Colossal Fibonacci Numbers!(快速幂+打表)
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
- UVA 11582 - Colossal Fibonacci Numbers!【大数幂取模】
- UVA-11582-Colossal Fibonacci Numbers!(规律+幂取模)
- UVA 11582 - Colossal Fibonacci Numbers!
- UVa 11582 - Colossal Fibonacci Numbers!
- UVA - 11582 Colossal Fibonacci Numbers!
- uva 11582 Colossal Fibonacci Numbers!
- UVa 11582 Colossal Fibonacci Numbers!
- UVA - 11582 Colossal Fibonacci Numbers!
- UVA 11582 - Colossal Fibonacci Numbers!
- Uva 11582 Colossal Fibonacci Numbers!
- uva 11582Colossal Fibonacci Numbers!
- 【UVa】[11582]Colossal Fibonacci Numbers!
- 质数筛(洛谷1865 A % B Problem)
- 洛谷1414 又是毕业季II
- 扩展欧几里得模板(洛谷1082 同余方程NOIP 2012 提高组 第二天 第一题)
- python流程控制语句
- C/C++中struct与class的区别
- 快速幂取模+数学知识(Colossal Fibonacci Numbers! uva 11582)
- 扩展欧几里得应用解不定方程(uva12169,Disgruntled Judge,NWERC 2008)
- 唯一分解定理 (Choose and Divide Uva10375)
- 唯一分解定理(Minimum Sum LCM,Uva10791)
- Java中不容易被注意到的地方
- 欧拉函数phi值的计算模板
- 欧拉函数phi的应用(洛谷2158 [SDOI2008]仪仗队)
- 数学方法(GCD XOR Uva12716)
- 字符串转换成整数+不用加减乘除做加法