Clever Y
Little Y finds there is a very interesting formula in mathematics:
XY mod Z = K
Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, could you figure out Y fast?
Input
Input data consists of no more than 20 test cases. For each test case, there would be only one line containing 3 integers X, Z, K (0 ≤ X, Z, K ≤ 109).
Input file ends with 3 zeros separated by spaces.
Output
For each test case output one line. Write "No Solution" (without quotes) if you cannot find a feasible Y (0 ≤ Y < Z). Otherwise output the minimum Y you find.
Sample Input
5 58 33
2 4 3
0 0 0
Sample Output
9
No Solution
Source
POJ Monthly--2007.07.08, Guo, Huayang
题目类型:形如 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 186 187 188 |
#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 >> A >> C >> B) { if (A == 0 && B == 0 && C == 0) break; 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; } |
- « 上一篇:poj3233
- poj3250:下一篇 »