Problem F: Colossal Fibonacci Numbers!
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 and b 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
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define ull unsigned long long int const int maxn = 1010; vector<int> F[maxn]; int C[maxn] , n; string a; ull b; void get_cycle(int mod){ F[mod].push_back(0%mod); F[mod].push_back(1%mod); int i = 2; while(true){ F[mod].push_back((F[mod][i-1]+F[mod][i-2])%mod); if(F[mod][i] == F[mod][1] && F[mod][i-1] == F[mod][0]){ C[mod] = i-1; return; } i++; } } void ini(){ for(int i = 1; i <= 1000; i++){ get_cycle(i); } } int deal_a(){ int len = a.length() , r = 0 , mod = C[n]; for(int i = 0; i < len; i++){ r = (r*10%mod+(a[i]-'0')%mod)%mod; } return r; } string dec_to_two(ull num){ string result; while(num != 0){ result.push_back(char(num%2+'0')); num = num/2; } return result; } int deal_b(int r , string temp){ int len = temp.length() , ans = 1 , mod = C[n]; for(int i = 0; i < len; i++){ if(temp[i] == '1') ans = (ans*r)%mod; r = (r*r)%mod; } return ans; } void computing(){ int remaider = deal_a(); string temp = dec_to_two(b); remaider = deal_b(remaider , temp); printf("%d\n" , F[n][remaider]); } int main(){ ini(); int t; scanf("%d" , &t); while(t--){ cin >> a >> b >> n; computing(); } return 0; }