思路:
前两题题面相同,代码也相同,就只贴一题的题面了。这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题。
SPOJ3105题目链接:https://www.spoj.com/problems/MOD/
POJ3243题目链接:http://poj.org/problem?id=3243
题目:
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 #include <unordered_map> 15 using namespace std; 16 17 typedef long long LL; 18 typedef pair<LL, LL> pLL; 19 typedef pair<LL, int> pli; 20 typedef pair<int, LL> pil;; 21 typedef pair<int, int> pii; 22 typedef unsigned long long uLL; 23 24 #define lson i<<1 25 #define rson i<<1|1 26 #define lowbit(x) x&(-x) 27 #define bug printf("*********\n"); 28 #define debug(x) cout<<"["<<x<<"]" <<endl; 29 #define FIN freopen("D://code//in.txt", "r", stdin); 30 #define IO ios::sync_with_stdio(false),cin.tie(0); 31 32 const double eps = 1e-8; 33 const int mod = 1e9 + 7; 34 const int maxn = 1e6 + 7; 35 const double pi = acos(-1); 36 const int inf = 0x3f3f3f3f; 37 const LL INF = 0x3f3f3f3f3f3f3f3f; 38 39 int x, z, k; 40 unordered_map<LL, int> mp; 41 42 int Mod_Pow(int x, int n, int mod) { 43 int res = 1; 44 while(n) { 45 if(n & 1) res = (LL)res * x % mod; 46 x = (LL)x * x % mod; 47 n >>= 1; 48 } 49 return res; 50 } 51 52 int gcd(int a, int b) { 53 return b == 0 ? a : gcd(b, a % b); 54 } 55 56 int EXBSGS(int A, int B, int C) { 57 A %= C, B %= C; 58 if(B == 1) return 0; 59 int cnt = 0; 60 LL t = 1; 61 for(int g = gcd(A, C); g != 1; g = gcd(A, C)) { 62 if(B % g) return -1; 63 C /= g, B /= g, t = t * A / g % C; 64 cnt++; 65 if(B == t) return cnt; 66 } 67 mp.clear(); 68 int m = ceil(sqrt(1.0*C)); 69 LL base = B; 70 for(int i = 0; i < m; i++) { 71 mp[base] = i; 72 base = base * A % C; 73 } 74 base = Mod_Pow(A, m, C); 75 LL nw = t; 76 for(int i = 1; i <= m; i++) { 77 nw = nw * base % C; 78 if(mp.count(nw)) { 79 return i * m - mp[nw] + cnt; 80 } 81 } 82 return -1; 83 } 84 85 int main() { 86 //FIN; 87 while(~scanf("%d%d%d", &x, &z, &k)) { 88 if(x == 0 && z == 0 && k == 0) break; 89 int ans = EXBSGS(x, k, z); 90 if(ans == -1) printf("No Solution\n"); 91 else printf("%d\n", ans); 92 } 93 return 0; 94 }
Gym 101853G题目链接:http://codeforces.com/gym/101853/problem/G
代码实现如下:
1 #include <set> 2 #include <map> 3 #include <queue> 4 #include <stack> 5 #include <cmath> 6 #include <bitset> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstdlib> 11 #include <cstring> 12 #include <iostream> 13 #include <algorithm> 14 #include <unordered_map> 15 using namespace std; 16 17 typedef long long LL; 18 typedef pair<LL, LL> pLL; 19 typedef pair<LL, int> pli; 20 typedef pair<int, LL> pil;; 21 typedef pair<int, int> pii; 22 typedef unsigned long long uLL; 23 24 #define lson i<<1 25 #define rson i<<1|1 26 #define lowbit(x) x&(-x) 27 #define bug printf("*********\n"); 28 #define debug(x) cout<<"["<<x<<"]" <<endl; 29 #define FIN freopen("D://code//in.txt", "r", stdin); 30 #define IO ios::sync_with_stdio(false),cin.tie(0); 31 32 const double eps = 1e-8; 33 const int mod = 1e9 + 7; 34 const int maxn = 1e6 + 7; 35 const double pi = acos(-1); 36 const int inf = 0x3f3f3f3f; 37 const LL INF = 0x3f3f3f3f3f3f3f3f; 38 39 int t, a, b, m; 40 unordered_map<LL, int> mp; 41 42 LL Mod_Pow(LL x, LL n, LL mod) { 43 LL res = 1; 44 while(n) { 45 if(n & 1) res = res * x % mod; 46 x = x * x % mod; 47 n >>= 1; 48 } 49 return res; 50 } 51 52 int gcd(int a, int b) { 53 return b == 0 ? a : gcd(b, a % b); 54 } 55 56 LL EXBSGS(int A, int B, int C) { 57 A %= C, B %= C; 58 if(B == 1) return 0; 59 int cnt = 0; 60 LL t = 1; 61 for(int g = gcd(A, C); g != 1; g = gcd(A, C)) { 62 if(B % g) return -1; 63 C /= g, B /= g; 64 t = t * A / g % C; 65 cnt++; 66 if(B == t) return cnt; 67 } 68 mp.clear(); 69 int m = ceil(sqrt(1.0 * C)); 70 LL base = B; 71 for(int i = 0; i < m; i++) { 72 mp[base] = i; 73 base = base * A % C; 74 } 75 base = Mod_Pow(A, m, C); 76 LL nw = t; 77 for(int i = 1; i <= m + 1; i++) { 78 nw = base * nw % C; 79 if(mp.count(nw)) { 80 return i * m - mp[nw] + cnt; 81 } 82 } 83 return -1; 84 } 85 86 int main() { 87 scanf("%d", &t); 88 while(t--) { 89 scanf("%d%d%d", &a, &b, &m); 90 LL ans = EXBSGS(a, b, m); 91 printf("%lld\n", ans); 92 } 93 return 0; 94 }
原文地址:https://www.cnblogs.com/Dillonh/p/9512030.html
时间: 2024-10-13 11:36:18