由PQ互为逆元得到
(
P
∗
Q
)
m
o
d
m
o
d
=
1
(P*Q) \bmod mod=1
( P ∗ Q ) mod m o d = 1 ,因此
(
P
∗
Q
−
1
)
m
o
d
m
o
d
=
0
(P*Q-1) \bmod mod=0
( P ∗ Q − 1 ) mod m o d = 0 ,即,
P
∗
Q
−
1
=
k
∗
m
o
d
P*Q-1=k*mod
P ∗ Q − 1 = k ∗ m o d ,因此,mod为P*Q-1的因子 由题意,有mod为质数,所以mod为P*Q-1的质因子,又有mod>P,mod>Q,就能得出结论,mod是P*Q-1的最大质因子
因此,找到P*Q-1的第一个满足它的另一个因子是质数的因子即可
# include <iostream>
# include <cmath>
# define endl '\n'
using namespace std;
typedef long long ll;
bool isprime ( ll x) {
if ( x <= 1 ) return false ;
for ( ll i = 2 ; i <= sqrt ( x) ; ++ i) {
if ( x % i == 0 ) return false ;
}
return true ;
}
int main ( ) {
ios:: sync_with_stdio ( false ) ; cin. tie ( 0 ) ; cout. tie ( 0 ) ;
int _; cin >> _;
while ( _ -- ) {
ll p, q, en;
cin >> p >> q >> en;
ll pq1 = p * q - 1 ;
ll ans = - 1 ;
for ( ll i = 1 ; i <= sqrt ( pq1) ; ++ i) {
if ( pq1 % i == 0 ) {
if ( isprime ( pq1 / i) ) {
ans = pq1 / i;
break ;
}
}
}
if ( ans == - 1 ) cout << "shuanQ" << endl;
else cout << en * q % ans << endl;
}
}