Description
Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:
Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
Output
For each test case, output the answer with case number in a single line.
Sample Input
3 3 4 10 3 4 5 13 5 3 2 19 100
Sample Output
Case #1: 2 Case #2: 11Case #3: 12
先推出公式,答案就是a^fib(n-2)*b^fib(n-1)%p,然后根据欧拉定理降次即可。
#include<set> #include<map> #include<ctime> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define loop(i,j,k) for (int i = j;i != -1; i = k[i]) #define lson x << 1, l, mid #define rson x << 1 | 1, mid + 1, r #define ff first #define ss second #define mp(i,j) make_pair(i,j) #define pb push_back #define pii pair<int,LL> #define inone(x) scanf("%d", &x); #define intwo(x,y) scanf("%d%d", &x, &y); using namespace std; typedef unsigned long long LL; const int low(int x) { return x&-x; } const double eps = 1e-4; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 1e5 + 10; int T, a, b, p, n, cas = 0, f[32]; int c[2], d[2][2], e[2][2]; int phi(int x) { int res = 1; for (int i = 2; i*i <= x; i++) { if (x % i) continue; res *= i - 1; x /= i; while (x%i == 0) res *= i, x /= i; } return res * max(x - 1, 1); } int get(int x, int y, int z) { int res = 1; for (; y; y >>= 1) { if (y & 1) res = 1LL * res*x%z; x = 1LL * x*x%z; } return res%z; } int main() { f[0] = 0; f[1] = 1; rep(i, 2, 31) f[i] = f[i - 1] + f[i - 2]; inone(T); while (T--) { scanf("%d%d%d%d", &a, &b, &p, &n); if (n == 1) { printf("Case #%d: %d\n", ++cas, a%p); continue; } if (n == 2) { printf("Case #%d: %d\n", ++cas, b%p); continue; } if (n <= 32) { printf("Case #%d: %d\n", ++cas, 1LL * get(a, f[n - 2], p)*get(b, f[n - 1], p) % p); } else { int mod = phi(p); c[0] = 0; c[1] = 1; d[0][0] = 0; d[1][0] = 1; d[0][1] = 1; d[1][1] = 1; for (int x = n - 2; x; x >>= 1) { if (x & 1) { rep(i, 0, 1) { e[0][i] = 0; rep(j, 0, 1) e[0][i] += 1LL * c[j] * d[j][i] % mod; } rep(i, 0, 1) c[i] = e[0][i] % mod; } rep(i, 0, 1) rep(j, 0, 1) { e[i][j] = 0; rep(k, 0, 1) e[i][j] += 1LL * d[i][k] * d[k][j] % mod; } rep(i, 0, 1) rep(j, 0, 1) d[i][j] = e[i][j] % mod; } printf("Case #%d: %d\n", ++cas, 1LL * get(a, c[0] + mod, p)*get(b, c[1] + mod, p) % p); } } return 0; }