题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2629
题目大意:菲波那切数列求f(ab) % n,其中 a,b < 264 ,n < 1001.
解题思路:打表找规律。然后求出f[i]%n的周期之后快速幂取模。
坑点:unsigned long long!!
代码:
1 const int maxn = 1e6 + 10; 2 int f[maxn] = {0, 1}; 3 int n, m; 4 unsigned long long a, b; 5 6 unsigned long long pow_mod(unsigned long long x, unsigned long long y){ 7 if(y == 0) return 1; 8 ll tmp = pow_mod(x, y / 2) % m; 9 ll ans = tmp * tmp % m; 10 if(y & 1) ans = x % m * ans; 11 return ans % m; 12 } 13 void solve(){ 14 f[1] = 1 % n; 15 for(int i = 0; i <= n * n + 10; i++){ 16 if(i == 0 || i == 1) continue; 17 else{ 18 f[i] = f[i - 1] + f[i - 2]; 19 f[i] %= n; 20 if(f[i] == f[1] && f[i - 1] == f[0]) { 21 m = i - 1; 22 break; 23 } 24 } 25 } 26 int x = pow_mod(a, b); 27 printf("%d ", f[x]); 28 } 29 30 int main(){ 31 int t; 32 scanf("%d", &t); 33 while(t--){ 34 cin >> a >> b; 35 cin >> n; 36 solve(); 37 } 38 }
题目:
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 f(a b ) upon division by n. Sample Input 3 1 1 2 2 3 1000 18446744073709551615 18446744073709551615 1000 Sample Output 1 21 250