結果

問題 No.368 LCM of K-products
ユーザー 37zigen37zigen
提出日時 2016-04-30 11:31:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 2,386 bytes
コンパイル時間 3,454 ms
コンパイル使用メモリ 82,508 KB
実行使用メモリ 60,616 KB
最終ジャッジ日時 2023-10-23 21:14:44
合計ジャッジ時間 11,263 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 294 ms
56,316 KB
testcase_01 AC 307 ms
60,368 KB
testcase_02 AC 303 ms
58,364 KB
testcase_03 AC 314 ms
60,436 KB
testcase_04 AC 315 ms
60,432 KB
testcase_05 AC 290 ms
60,616 KB
testcase_06 AC 133 ms
57,292 KB
testcase_07 AC 140 ms
57,812 KB
testcase_08 AC 137 ms
57,432 KB
testcase_09 AC 139 ms
57,636 KB
testcase_10 AC 135 ms
57,524 KB
testcase_11 AC 136 ms
57,652 KB
testcase_12 AC 138 ms
57,500 KB
testcase_13 AC 272 ms
58,036 KB
testcase_14 AC 302 ms
56,468 KB
testcase_15 AC 301 ms
58,452 KB
testcase_16 AC 300 ms
58,496 KB
testcase_17 AC 285 ms
58,532 KB
testcase_18 AC 305 ms
60,348 KB
testcase_19 AC 207 ms
58,044 KB
testcase_20 AC 279 ms
58,188 KB
testcase_21 AC 192 ms
57,676 KB
testcase_22 AC 311 ms
60,448 KB
testcase_23 AC 139 ms
57,516 KB
testcase_24 AC 138 ms
55,276 KB
testcase_25 AC 139 ms
57,628 KB
testcase_26 AC 135 ms
57,348 KB
testcase_27 AC 141 ms
57,536 KB
testcase_28 AC 140 ms
57,568 KB
testcase_29 AC 139 ms
57,532 KB
testcase_30 AC 140 ms
57,592 KB
testcase_31 AC 142 ms
57,540 KB
testcase_32 AC 139 ms
57,428 KB
testcase_33 AC 226 ms
57,832 KB
testcase_34 AC 297 ms
60,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	int mod=1000000007;
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();
		Prime prime=new Prime();
		ArrayList<Integer> primeList=prime.primeList((int)Math.sqrt(1e9)+5);
		HashMap<Integer,ArrayList<Integer>>hm=new HashMap<>();
		for(int i=0;i<n;i++){
			int a=sc.nextInt();
			for(Factor f:prime.primeFactorF(primeList, a)){
				ArrayList<Integer> al=hm.get(f.base);
				if(al==null){
					al=new ArrayList<>();
				}
				al.add(f.exp);
				hm.put(f.base, al);
			}
		}
		long ans=1;
		for(Map.Entry<Integer,ArrayList<Integer>> e:hm.entrySet()){
			ArrayList<Integer> al=e.getValue();
			al.sort(Collections.reverseOrder());
			int max=Math.min(k,al.size());
			int sum=0;
			for(int i=0;i<max;i++){
				sum+=al.get(i);
			}
			ans=ans*pow(e.getKey(),sum,mod)%mod;
		}
		System.out.println(ans);
	}
	long pow(long a,long n,long mod){
		long ans=1;
		long pow=a;
		while(n>=1){
			if(n%2==0){
				pow=pow*pow;
				pow%=mod;
				n/=2;
			}else if(n%2==1){
				ans*=pow;
				ans%=mod;
				n--;
			}
		}
		return ans;
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}

	class Prime{
		boolean[] isPrimeArray(int max){
			boolean[] isPrime=new boolean[max+1];
			Arrays.fill(isPrime, true);
			isPrime[0]=isPrime[1]=false;
			for(int i=2;i*i<=max;i++){
				if(isPrime[i]){
					for(int j=2;j*i<=max;j++){
						isPrime[j*i]=false;
					}
				}
			}
			return isPrime;
		}
		ArrayList<Integer> primeList(int max){
			boolean[] isPrime=isPrimeArray(max);
			ArrayList<Integer> primeList=new ArrayList<Integer>();
			for(int i=2;i<=max;i++){
				if(isPrime[i]){
					primeList.add(i);
				}
			}
			return primeList;
		}
		ArrayList<Factor> primeFactorF(ArrayList<Integer> primeList,long num){
			ArrayList<Factor> ret=new ArrayList<Factor>();
			for(int p:primeList){
				int exp=0;
				while(num%p==0){
					num/=p;
					exp++;
				}
				if(exp>0)ret.add(new Factor(p,exp));
			}
			if(num>1)ret.add(new Factor((int)num,1));
			return ret;
		}
	}
	class Factor{
		int base,exp;
		Factor(int base,int exp){
			this.base=base;
			this.exp=exp;
		}
	}
}
0