[POJ 3243]Clever Y

时间:2023-03-09 07:56:35
[POJ 3243]Clever Y

Description

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

题解

扩展BSGS:

当模数 $c$ 不是质数的时候,显然不能直接使用 $BSGS$ 了,考虑它的扩展算法。

前提:同余性质。

令 $d = gcd(a, c)$ , $A = a \cdot d,B = b \cdot d, C = c \cdot d$

则 $a \cdot d \equiv b \cdot d \pmod{c \cdot d}$

等价于 $a \equiv b \pmod{c}$

因此我们可以先消除因子。

对于现在的问题 $(A \cdot d)^x \equiv B \cdot d \pmod{C \cdot d}$ 当我们提出 $d = gcd(a, c)$ ($d \neq 1$)后,原式化为 $A \cdot (A \cdot d)^{x-1} \equiv B \pmod{C}$ 。

即求 $D \cdot A^{x-cnt} \equiv B \pmod{C}$ ,令 $x = i \cdot r-j+cnt$ 。之后的做法就和 $BSGS$ 一样了。

值得注意的是因为这样求出来的解 $x \geq cnt$ 的,但有可能存在解 $x < cnt$ ,所以一开始需要特判。

 //It is made by Awson on 2018.1.15
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
const LL MOD = ;
void read(LL &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(LL x) {
if (x > ) write(x/);
putchar(x%+);
} LL a, b, c, ans;
struct MAP {
LL ha[MOD+]; int id[MOD+];
void clear() {for (int i = ; i < MOD; i++) ha[i] = id[i] = -; }
int count(LL x) {
LL pos = x%MOD;
while (true) {
if (ha[pos] == -) return ;
if (ha[pos] == x) return ;
++pos; if (pos >= MOD) pos -= MOD;
}
}
void insert(LL x, int idex) {
LL pos = x%MOD;
while (true) {
if (ha[pos] == - || ha[pos] == x) {ha[pos] = x, id[pos] = idex; return; }
++pos; if (pos >= MOD) pos -= MOD;
}
}
int query(LL x) {
LL pos = x%MOD;
while (true) {
if (ha[pos] == x) return id[pos];
++pos; if (pos >= MOD) pos -= MOD;
}
}
}mp; LL quick_pow(LL a, LL b, LL c) {
LL ans = ;
while (b) {
if (b&) ans = ans*a%c;
a = a*a%c, b >>= ;
}
return ans;
}
LL gcd(LL a, LL b) {return b ? gcd(b, a%b) : a; }
LL exBSGS(LL a, LL b, LL c) {
if (b == ) return ;
LL cnt = , d = , t;
while ((t = gcd(a, c)) != ) {
if (b%t) return -;
++cnt, b /= t, c /= t, d = d*(a/t)%c;
if (d == b) return cnt;
}
mp.clear();
LL tim = ceil(sqrt(c)), tmp = b%c;
for (int i = ; i <= tim; i++) {
mp.insert(tmp, i); tmp = tmp*a%c;
}
t = tmp = quick_pow(a, tim, c); tmp = (tmp*d)%c;
for (int i = ; i <= tim; i++) {
if (mp.count(tmp)) return tim*i-mp.query(tmp)+cnt;
tmp = tmp*t%c;
}
return -;
}
void work() {
while ((~scanf("%lld%lld%lld", &a, &c, &b))) {
if (c == ) return;
if ((ans = exBSGS(a%c, b%c, c)) == -) printf("No Solution\n");
else write(ans), putchar('\n');
}
}
int main() {
work();
return ;
}