結果

問題 No.1262 グラフを作ろう!
ユーザー fura
提出日時 2020-10-18 03:31:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 225 ms / 3,000 ms
コード長 550 bytes
コンパイル時間 1,840 ms
コンパイル使用メモリ 195,780 KB
最終ジャッジ日時 2025-01-15 11:27:25
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 96
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:9:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    9 |         int n; scanf("%*d%d",&n);
      |                ~~~~~^~~~~~~~~~~~
main.cpp:11:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |         rep(i,n) scanf("%d",&a[i]);
      |                  ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

int main(){
	int n; scanf("%*d%d",&n);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	vector<lint> freq(1e6+1);
	rep(i,n) freq[a[i]]++;

	vector<lint> dp(1e6+1); // dp[d]=(gcd(a[i],b)=d となる (i,b) の個数)
	for(int d=1e6;d>0;d--){
		for(int j=1;j*d<=1e6;j++){
			dp[d]+=j*freq[j*d];
			if(j>=2) dp[d]-=dp[j*d];
		}
	}

	lint ans=0;
	for(int d=1;d<=1e6;d++) ans+=dp[d]*d;
	rep(i,n) ans-=a[i];
	printf("%lld\n",ans);

	return 0;
}
0