题意
给定 ,满足
且 , 有且只有两个质因子,求 。
思路
也就是说我们要用 和 凑出 。
看到 这个性质,所以我们可以求出一个方程 的整数解 ,这样就可以用 凑出 。这可以用扩展欧几里得算法求出。
然而我们发现 与 可能有一个为负数,假设 为负数。那么我们需要求出 即 ,也就是 在模 意义下的逆元。
但是 并不为质数,若使用欧拉定理的话需要找到其质因数会爆炸,所以我们再次使用扩展欧几里得算法:
发现 ,于是有 ,可以求出一组整数解 。注意此时求出的逆元并不一定是正整数,注意取模。
你也许需要使用 __int128 或者龟速乘。
代码
#include <bits/stdc++.h>
using namespace std;
typedef __int128 ll;
ll s1, s2, k1, k2, c, T, p[100002], len, m;
bitset<10000002> f;
ll qmi(ll x, ll y, ll z) {
x %= z;
ll res = 1 % z;
while (y) res = res * (y & 1 ? x : 1) % z, x = x * x % z, y >>= 1;
return res;
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) return x = 1, y = 0, a;
ll d = exgcd(b, a % b, x, y), z = x;
x = y, y = z - y * (a / b);
return d;
}
int main() {
long long tt;
cin >> tt, T = tt;
while (T -- ) {
cin >> tt, s1 = tt;
cin >> tt, s2 = tt;
cin >> tt, k1 = tt;
cin >> tt, k2 = tt;
cin >> tt, c = tt;
ll c1, c2, t;
exgcd(k1, k2, c1, c2);
if (c1 < 0) {
c1 = -c1;
s1 = qmi(s1, c1, c);
exgcd(s1, c, s1, t);
s1 = (s1 % c + c) % c;
}
else s1 = qmi(s1, c1, c);
if (c2 < 0) {
c2 = -c2;
s2 = qmi(s2, c2, c);
exgcd(s2, c, s2, t);
s2 = (s2 % c + c) % c;
}
else s2 = qmi(s2, c2, c);
tt = s1 * s2 % c;
cout << tt << "\n";
}
}