标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2560 Accepted Submission(s): 657
3
3 4 10 3
4 5 13 5
3 2 19 100
/** 题意:根据题意可以知道求 f(n) = f(n-1)*f(n-2)的值 f(1) = a f(2) = b; f(3) = a*b; f(4) = a*b^2 f(5) = a^2*b^3 ...... 可以得知 a,b 的指数是斐波纳锲数列 做法:欧拉 + 矩阵 + 蒙哥马利幂模算法
ps:没有搞清楚一点 在求矩阵的n-3次幂可以过 可是n-2次幂 不能过 **/ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define SIZE 2 #define clr( a, b ) memset( a, b, sizeof(a) ) long long MOD; struct Mat { long long mat[ SIZE ][ SIZE ]; int n; Mat(int _n) { n = _n; clr(mat, 0); } void init() { for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) { mat[i][j] = (i == j); } } Mat operator * (const Mat& b) const { Mat c(b.n); for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) { if(mat[i][k]) for(int j = 0; j < n; ++j) { c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD; } } return c; } }; Mat fast_mod(Mat a, int b) { Mat res(a.n); res.init(); while(b) { if(b & 1) { res = res * a; } a = a * a; b >>= 1; } return res; } long long eular(long long n) { long long ans = n; for(int i = 2; i * i <= n; i++) { if(n % i == 0) { ans -= ans / i; while(n % i == 0) { n /= i; } } } if(n > 1) { ans -= ans / n; } return ans; } long long modPow(long long s, long long index, long long mod) { long long ans = 1; s %= mod; while(index >= 1) { if((index & 1) == 1) { //奇数 ans = (ans * s) % mod; } index >>= 1; s = s * s % mod; } return ans; } int main() { int T; int Case = 1; scanf("%d", &T); while(T--) { long long x, y, n, res, p; cin >> x >> y >> p >> n; printf("Case #%d: ", Case++); if(p == 0 || p == 1) { printf("0\n"); continue; } if(n == 1) { cout << x % p << endl; continue; } else if(n == 2) { cout << y % p << endl; continue; } MOD = eular(p); Mat C(2); C.mat[0][0] = 0; C.mat[0][1] = 1; C.mat[1][0] = 1; C.mat[1][1] = 1; C = fast_mod(C, n - 3); long long aa = C.mat[0][0] + C.mat[0][1]; long long bb = C.mat[1][1] + C.mat[1][0]; res = ((modPow(x , aa, p) * modPow(y , bb, p)) % p + p) % p; cout << res << endl; } return 0; }
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define SIZE 2 #define clr( a, b ) memset( a, b, sizeof(a) ) long long MOD; struct Mat { long long mat[ SIZE ][ SIZE ]; int n; Mat(int _n) { n = _n; clr(mat, 0); } void init() { for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) { mat[i][j] = (i == j); } } Mat operator * (const Mat& b) const { Mat c(b.n); for(int k = 0; k < n; ++k) for(int i = 0; i < n; ++i) { if(mat[i][k]) for(int j = 0; j < n; ++j) { c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD; } } return c; } }; Mat fast_mod(Mat a, int b) { Mat res(a.n); res.init(); while(b) { if(b & 1) { res = res * a; } a = a * a; b >>= 1; } return res; } long long eular(long long n) { long long ans = n; for(int i = 2; i * i <= n; i++) { if(n % i == 0) { ans -= ans / i; while(n % i == 0) { n /= i; } } } if(n > 1) { ans -= ans / n; } return ans; } long long modPow(long long s, long long index, long long mod) { long long ans = 1; s %= mod; while(index >= 1) { if((index & 1) == 1) { //奇数 ans = (ans * s) % mod; } index >>= 1; s = s * s % mod; } return ans; } int main() { int T; int Case = 1; scanf("%d", &T); while(T--) { long long x, y, n, res, p; cin >> x >> y >> p >> n; printf("Case #%d: ", Case++); if(p == 0 || p == 1) { printf("0\n"); continue; } if(n == 1) { cout << x % p << endl; continue; } else if(n == 2) { cout << y % p << endl; continue; } MOD = eular(p); Mat C(2); C.mat[0][0] = 1; C.mat[0][1] = 1; C.mat[1][0] = 1; C.mat[1][1] = 0; C = fast_mod(C, n - 3); long long aa = C.mat[1][0] + C.mat[1][1]; long long bb = C.mat[0][0] + C.mat[0][1]; res = ((modPow(x , aa, p) * modPow(y , bb, p)) % p + p) % p; cout << res << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4855218.html