結果

問題 No.1140 EXPotentiaLLL!
ユーザー shojin_proshojin_pro
提出日時 2020-07-31 21:55:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 3,935 bytes
コンパイル時間 4,221 ms
コンパイル使用メモリ 77,340 KB
実行使用メモリ 85,640 KB
最終ジャッジ日時 2023-09-20 23:04:40
合計ジャッジ時間 11,407 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 674 ms
85,100 KB
testcase_01 AC 690 ms
85,472 KB
testcase_02 AC 686 ms
85,640 KB
testcase_03 AC 655 ms
85,616 KB
testcase_04 AC 607 ms
85,348 KB
testcase_05 AC 704 ms
85,220 KB
testcase_06 AC 707 ms
85,348 KB
testcase_07 AC 730 ms
85,392 KB
testcase_08 AC 143 ms
78,544 KB
testcase_09 AC 143 ms
67,228 KB
testcase_10 AC 139 ms
80,304 KB
testcase_11 AC 141 ms
78,480 KB
testcase_12 AC 144 ms
66,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        FastScanner sc = new FastScanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        Sieve s = new Sieve((int)5e6);
        int t = sc.nextInt();
        for(int i = 0; i < t; i++){
            long a = sc.nextLong();
            int p = sc.nextInt();
            if(!s.isPrime(p)){
                pw.println(-1);
            }else{
                pw.println(a%p != 0 ? 1 : 0);  
            }
            
        }
        pw.flush();
    }
    
    private static long rep2(long b, long n, long mod){
        if(n == 0) return 1;
        long bn = rep2(b,n/2,mod);
        if(n % 2 == 0){
            return (bn*bn)%mod;
        }else{
            return (bn*bn)%mod*b%mod;
        }
    }
}


class Sieve{
    static int n;
    static int[] f;
    static ArrayList<Integer> prime;
    public Sieve(int n){
        long ln = n;
        prime = new ArrayList<Integer>();
        f = new int[n+1];
        f[0] = f[1] = -1;
        for(int i = 2; i <= n; i++){
            if(f[i] != 0){
                continue;
            }
            f[i] = i;
            prime.add(i);
            long li = (long)i;
            for(long j = li*li; j <= ln; j += li){
                if(f[(int)j] == 0){
                    f[(int)j] = i;
                }
            }
        }
    }
    
    public static boolean isPrime(int x){
        return f[x] == x;
    }
    
    public static ArrayList<Integer> factorList(int x){
        ArrayList<Integer> res = new ArrayList<Integer>();
        while(x != 1){
            res.add(f[x]);
            x /= f[x];
        }
        return res;
    }
    
    public static HashMap<Integer,Integer> factor(int x){
        ArrayList<Integer> fl = factorList(x);
        HashMap<Integer,Integer> res = new HashMap<Integer,Integer>();
        if(fl.size()==0){
            return new HashMap<Integer,Integer>();
        }
        int prev = fl.get(0);
        int cnt = 0;
        for(int p : fl){
            if(prev == p){
                cnt++;
            }else{
                res.put(prev,cnt);
                prev = p;
                cnt = 1;
            }
        }
        res.put(prev,cnt);
        return res;
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;
    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
         return Double.parseDouble(next());
    }
    
    public String[] nextArray(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = next();
        return a;
    }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    } 
}
0