結果
| 問題 |
No.1556 Power Equality
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-07-04 14:48:04 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 62 ms / 2,000 ms |
| コード長 | 2,067 bytes |
| コンパイル時間 | 2,172 ms |
| コンパイル使用メモリ | 78,912 KB |
| 実行使用メモリ | 37,560 KB |
| 最終ジャッジ日時 | 2024-07-04 14:48:09 |
| 合計ジャッジ時間 | 3,424 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int a = sc.nextInt();
int b = sc.nextInt();
int[] mods = new int[10];
int idx = 0;
int value = 1000000000;
while (idx < 10) {
if (isPrime(value)) {
mods[idx++] = value;
}
value++;
}
for (int i = 0; i < 10; i++) {
if (powmod(a, b, mods[i]) != powmod(b, a, mods[i])) {
System.out.println("No");
return;
}
}
System.out.println("Yes");
}
static long powmod(long x, int p, int mod) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
return powmod(x * x % mod, p / 2, mod);
} else {
return powmod(x, p - 1, mod) * x % mod;
}
}
static boolean isPrime(int x) {
for (int i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
tenten