快速幂取模+数学知识(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