結果

問題 No.368 LCM of K-products
ユーザー 37zigen37zigen
提出日時 2016-04-30 11:23:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,314 bytes
コンパイル時間 2,663 ms
コンパイル使用メモリ 88,624 KB
実行使用メモリ 57,284 KB
最終ジャッジ日時 2024-04-15 08:05:06
合計ジャッジ時間 11,379 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 320 ms
57,284 KB
testcase_02 AC 303 ms
54,984 KB
testcase_03 AC 315 ms
57,012 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 138 ms
54,268 KB
testcase_07 AC 141 ms
54,208 KB
testcase_08 AC 140 ms
54,080 KB
testcase_09 AC 141 ms
54,244 KB
testcase_10 AC 142 ms
54,096 KB
testcase_11 AC 141 ms
53,864 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 140 ms
53,844 KB
testcase_24 AC 140 ms
54,000 KB
testcase_25 AC 144 ms
54,112 KB
testcase_26 AC 141 ms
54,000 KB
testcase_27 AC 145 ms
54,124 KB
testcase_28 AC 141 ms
54,096 KB
testcase_29 AC 143 ms
54,144 KB
testcase_30 AC 145 ms
54,048 KB
testcase_31 AC 145 ms
54,092 KB
testcase_32 AC 143 ms
53,888 KB
testcase_33 WA -
testcase_34 AC 292 ms
57,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
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));
		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));
			}
			return ret;
		}
	}
	class Factor{
		int base,exp;
		Factor(int base,int exp){
			this.base=base;
			this.exp=exp;
		}
	}
}
0