题目收集
题目列表题单
返回列表

Crack RSA

CodeforcesP0012· 3000ms· 1024 MB· ZJU Summer 2026 Contest 1原题链接
数论

创建于 2026/07/16 · 更新于 2026/07/16

题目描述

Alan233's company deployed an RSA encryption system for all employees, but the security department, cutting corners, decided to let all employees share the same modulus NNN, assigning only different (ei,di)(e_i, d_i)(ei​,di​) key pairs.

Employee P received their own key (e,d)(e, d)(e,d) and somehow managed to hack into the company's system. While everyone was shocked, P explained nonchalantly: "I factored NNN."

Now, play the role of P and reproduce this attack. Given TTT test cases, factor NNN for each case and output ppp and qqq.

Note that the basic RSA workflow is as follows:

  • Pick two distinct large primes ppp and qqq, and compute the modulus N=p×qN = p \times qN=p×q;
  • Compute Euler's totient function φ(N)=(p−1)×(q−1)\varphi(N) = (p-1) \times (q-1)φ(N)=(p−1)×(q−1);
  • Choose eee coprime to φ(N)\varphi(N)φ(N) as the public exponent;
  • Compute ddd satisfying e×d≡1(modφ(N))e \times d \equiv 1 \pmod{\varphi(N)}e×d≡1(modφ(N)) as the private exponent;
  • (N,e)(N, e)(N,e) is the public key and (N,d)(N, d)(N,d) is the private key. Encryption computes c=me mod Nc = m^e \bmod Nc=memodN, and decryption computes m=cd mod Nm = c^d \bmod Nm=cdmodN.

Formally, you're given N,e,dN,e,dN,e,d satifying the above condition, you need to find out the value of ppp and qqq.

输入格式

The first line contains an integer TTT (1≤T≤101\le T \le 101≤T≤10). The next TTT lines each contain three integers N,e,dN, e, dN,e,d(15≤N<225615\le N \lt 2^{256}15≤N<2256, 1<e,d<φ(N)1 \lt e,d \lt \varphi(N)1<e,d<φ(N), N=p×qN = p \times qN=p×q (p,qp, qp,q are primes), e×d≡1(modφ(N))e \times d \equiv 1 \pmod{\varphi(N)}e×d≡1(modφ(N)), and 3≤p<q<21283\le p \lt q \lt 2^{128}3≤p<q<2128).

输出格式

For each test case, output one line containing two integers p,qp, qp,q such that p×q=Np \times q = Np×q=N and p<qp \lt qp<q.

样例输入 #1

3
15 3 3
25777 3 16971
1005973 5 602381

样例输出 #1

3 5
149 173
997 1009