結果
問題 |
No.917 Make One With GCD
|
ユーザー |
|
提出日時 | 2020-03-09 17:23:28 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 175 ms / 2,000 ms |
コード長 | 951 bytes |
コンパイル時間 | 2,199 ms |
コンパイル使用メモリ | 78,004 KB |
実行使用メモリ | 55,636 KB |
最終ジャッジ日時 | 2024-06-24 11:15:17 |
合計ジャッジ時間 | 8,384 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 32 |
ソースコード
import java.util.ArrayList; import java.util.Scanner; class Main { public static void main(String[] args) { new Main().run(); } int gcd(int a,int b) { return a==0?b:gcd(b%a,a); } void run() { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;++i)a[i]=sc.nextInt(); long ng=0; for(int i=0;i<n;++i) { if(a[i]==1)continue; ArrayList<Integer> divs=new ArrayList<>(); for(int div=2;div*div<=a[i];++div) { int sz=0; while(a[i]%div==0) { a[i]/=div; ++sz; } if(sz>0) { divs.add(div); } } if(a[i]>1)divs.add(a[i]); long add=0; for(int s=1;s<1<<divs.size();++s) { long g=1; int sz=0; for(int shift=0;shift<divs.size();++shift)if((s&(1<<shift))>0)g*=divs.get(shift); for(int j=i+1;j<n;++j)if(a[j]%g==0)++sz; add+=(1L<<sz)*(Integer.bitCount(s)%2==1?1:-1); } ng+=add; } System.out.println((1L<<n)-1-ng); } }