結果

問題 No.1022 Power Equation
ユーザー 37zigen37zigen
提出日時 2020-03-09 07:00:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 2,537 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 74,696 KB
実行使用メモリ 52,220 KB
最終ジャッジ日時 2023-08-20 19:33:11
合計ジャッジ時間 3,444 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,756 KB
testcase_01 AC 57 ms
50,264 KB
testcase_02 AC 56 ms
50,288 KB
testcase_03 AC 60 ms
50,196 KB
testcase_04 AC 82 ms
52,020 KB
testcase_05 AC 85 ms
52,076 KB
testcase_06 AC 83 ms
52,180 KB
testcase_07 AC 84 ms
52,220 KB
testcase_08 AC 82 ms
52,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.NoSuchElementException;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	long pow(long a,long n) {
		if(a==0)return 0;
		long ret=1;
		for(;n>0;n/=2,a*=a)if(n%2==1)ret*=a;
		return ret;
	}
	
	long gcd(long a,long b) {
		if(a>b)return gcd(b,a);
		if(a==0)return b;
		return gcd(b%a,a);
	}
	
	long solve(long N) {
		long ret=(N-1)*N+N*N;//a^b=a^b (a!=1&&b!=1), 1^a=1^b, はすでに含む。
		int e=30;
		int[] cnt=new int[e+1];
		for(int u=1;u<=e;++u) {
			for(int v=1;v<=e;++v) {
				if(gcd(u,v)!=1||(u==1&&v==1))continue;
				cnt[Math.max(u, v)]+=N/Math.max(u, v);
			}
		}
		{
			long x=(long)Math.sqrt(N+1);
			for(int i=2;i<=e;++i) {
				while(pow(x,i)>N)--x;
				ret+=(x-1)*cnt[i];
			}
		}
		return ret;
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		PrintWriter pw=new PrintWriter(System.out);
		int T=sc.nextInt();
		long[] N=new long[T];
		for(int i=0;i<T;++i) {
			N[i]=sc.nextLong();
			pw.println(solve(N[i]));
		}
		pw.close();
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public int nextInt() {
    	return Integer.parseInt(next());
    }
    
    public long nextLong() {
    	return Long.parseLong(next());
    }
}
0