結果
問題 |
No.8056 量子コンピュータで素因数分解 Easy
|
ユーザー |
|
提出日時 | 2020-01-27 18:35:53 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 350 ms / 2,000 ms |
コード長 | 1,157 bytes |
コンパイル時間 | 4,403 ms |
コンパイル使用メモリ | 80,044 KB |
実行使用メモリ | 73,156 KB |
平均クエリ数 | 2.50 |
最終ジャッジ日時 | 2024-12-31 19:18:49 |
合計ジャッジ時間 | 13,180 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
import java.util.Scanner; import java.util.Random; import java.io.IOException; import java.math.BigInteger; public class ShorSolve { static BigInteger n; static BigInteger randomBigInteger(){ int bitlen = n.bitLength(); BigInteger r; Random rnd = new Random(); do { r = new BigInteger(bitlen, rnd); } while(r.equals(BigInteger.ZERO) || r.compareTo(n) >= 0); return r; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); n = sc.nextBigInteger(); while(true){ BigInteger a = randomBigInteger(); BigInteger b = n.gcd(a); if(!b.equals(BigInteger.ONE)){ System.out.println("! " + b + " " + n.divide(b)); break; } System.out.println("? " + a); BigInteger r = sc.nextBigInteger(); if(r.testBit(0)){ continue; } r = r.shiftRight(1); a = a.modPow(r, n); if(a.equals(n.subtract(BigInteger.ONE))){ continue; } BigInteger p = n.gcd(a.add(BigInteger.ONE)); BigInteger q = n.divide(p); System.out.println("! " + p + " " + q); break; } } }