結果

問題 No.3056 量子コンピュータで素因数分解 Easy
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2020-01-27 18:35:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 377 ms / 2,000 ms
コード長 1,157 bytes
コンパイル時間 3,531 ms
コンパイル使用メモリ 76,360 KB
実行使用メモリ 76,112 KB
平均クエリ数 2.31
最終ジャッジ日時 2023-08-30 06:42:53
合計ジャッジ時間 12,137 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 197 ms
72,336 KB
testcase_01 AC 200 ms
72,536 KB
testcase_02 AC 287 ms
74,584 KB
testcase_03 AC 306 ms
73,644 KB
testcase_04 AC 209 ms
72,572 KB
testcase_05 AC 211 ms
73,264 KB
testcase_06 AC 211 ms
71,220 KB
testcase_07 AC 222 ms
73,036 KB
testcase_08 AC 218 ms
72,860 KB
testcase_09 AC 219 ms
73,128 KB
testcase_10 AC 222 ms
73,128 KB
testcase_11 AC 231 ms
72,548 KB
testcase_12 AC 226 ms
73,168 KB
testcase_13 AC 229 ms
72,984 KB
testcase_14 AC 228 ms
72,520 KB
testcase_15 AC 377 ms
74,664 KB
testcase_16 AC 242 ms
72,584 KB
testcase_17 AC 244 ms
73,192 KB
testcase_18 AC 242 ms
72,724 KB
testcase_19 AC 270 ms
72,764 KB
testcase_20 AC 290 ms
73,560 KB
testcase_21 AC 236 ms
73,320 KB
testcase_22 AC 247 ms
72,664 KB
testcase_23 AC 317 ms
74,940 KB
testcase_24 AC 272 ms
76,112 KB
testcase_25 AC 191 ms
72,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }
  }
}
0