結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
39,448 KB
testcase_01 AC 129 ms
39,748 KB
testcase_02 AC 132 ms
39,404 KB
testcase_03 AC 131 ms
39,872 KB
testcase_04 AC 152 ms
39,584 KB
testcase_05 AC 147 ms
39,420 KB
testcase_06 AC 143 ms
39,640 KB
testcase_07 AC 144 ms
39,916 KB
testcase_08 AC 151 ms
39,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	long pow(long a,long n) {
		return n==0?1:(pow(a*a,n/2)*(n%2==1?a:1));
	}
	
	long gcd(long a,long b) {
		return a==0?b:gcd(b%a,a);
	}
	
	long solve(long N) {
		long ret=(N-1)*N+N*N;
		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() {
		Scanner sc=new Scanner(System.in);
		int T=sc.nextInt();
		if(!(1<=T&&T<=50))throw new AssertionError();
		long[] N=new long[T];
		for(int i=0;i<T;++i) {
			N[i]=sc.nextLong();
			if(!(1<=N[i]&&N[i]<=1e9))throw new AssertionError();
			System.out.println(solve(N[i]));
		}
	}
}


0