Discrete Logging
Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that
BL == N (mod P)
Input
Read several lines of input, each containing P,B,N separated by a space.
Output
For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print "no solution".
Sample Input
5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111
Sample Output
0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587
Hint
The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat's theorem that states
B(P-1) == 1 (mod P)
for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m
B(-m) == B(P-1-m) (mod P) .
Source
题目类型:形如 A ^ X = B (mod C)的高次同余方程
算法分析:直接按照A ^ X = B (mod C)的格式求解即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 110000 + 66; struct HashNode { long long data, id, next; }; HashNode Hash[maxn<<1]; bool flag[maxn<<1]; long long top; void Insert (long long a, long long b) { long long k = b & maxn; if (flag[k] == false) { flag[k] = true; Hash[k].next = -1; Hash[k].id = a; Hash[k].data = b; return ; } while (Hash[k].next != -1) { if (Hash[k].data == b) return ; k = Hash[k].next; } if (Hash[k].data == b) return ; Hash[k].next = ++top; Hash[top].next = -1; Hash[top].id = a; Hash[top].data = b; } long long Find (long long b) { long long k = b & maxn; if (flag[k] == false) return -1; while (k != -1) { if (Hash[k].data == b) return Hash[k].id; k = Hash[k].next; } return -1; } long long gcd (long long a, long long b) { return b ? gcd (b, a % b) : a; } long long gcd_ex (long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = gcd_ex (b, a % b, y, x); y = y - a / b * x; return d; } long long q_mul_mod (long long a, long long b, long long p) { long long ans = 1; while (b) { if (b & 1) { ans = ((ans % p) * (a % p)) % p; b--; } b = b >> 1; a = ((a % p) * (a % p)) % p; } return ans; } long long BabyStep_GiantStep (long long A, long long B, long long C) { top = maxn; B %= C; long long tmp = 1, i; for (i = 0; i <= 100; tmp = tmp * A % C, i++) if (tmp == B % C) return i; long long D = 1, cnt = 0; while ((tmp = gcd(A, C)) != 1) { if(B % tmp) return -1; C /= tmp; B /= tmp; D = D * A / tmp % C; cnt++; } long long M = (long long) ceil (sqrt (C + 0.0)); for (tmp = 1, i = 0; i <= M; tmp = tmp * A % C, i++) Insert (i, tmp); long long x, y, K = q_mul_mod (A, M, C); for (i = 0; i <= M; i++) { gcd_ex (D, C, x, y); // D * X = 1 ( mod C ) tmp = ((B * x) % C + C) % C; if((y = Find(tmp)) != -1) return i * M + y + cnt; D = D * K % C; } return -1; } int main() { // ifstream cin ("aaa.txt"); long long A, B, C; while(cin >> C >> A >> B) { memset (flag, 0, sizeof(flag)); long long ans = BabyStep_GiantStep (A, B, C); if (ans == -1) cout << "no solution" << endl; else cout << ans << endl; } return 0; } |
- « 上一篇:poj2406
- poj2456:下一篇 »