結果

問題 No.1262 グラフを作ろう!
ユーザー furafura
提出日時 2020-10-17 21:16:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 692 ms / 3,000 ms
コード長 1,895 bytes
コンパイル時間 3,376 ms
コンパイル使用メモリ 214,488 KB
実行使用メモリ 23,436 KB
最終ジャッジ日時 2023-09-28 08:35:37
合計ジャッジ時間 35,501 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 403 ms
18,812 KB
testcase_01 AC 296 ms
17,768 KB
testcase_02 AC 456 ms
18,676 KB
testcase_03 AC 320 ms
17,128 KB
testcase_04 AC 71 ms
12,244 KB
testcase_05 AC 270 ms
17,004 KB
testcase_06 AC 52 ms
11,744 KB
testcase_07 AC 80 ms
12,584 KB
testcase_08 AC 351 ms
17,488 KB
testcase_09 AC 216 ms
14,696 KB
testcase_10 AC 236 ms
15,076 KB
testcase_11 AC 365 ms
17,668 KB
testcase_12 AC 374 ms
18,664 KB
testcase_13 AC 337 ms
16,860 KB
testcase_14 AC 221 ms
14,652 KB
testcase_15 AC 170 ms
14,400 KB
testcase_16 AC 368 ms
17,620 KB
testcase_17 AC 335 ms
17,764 KB
testcase_18 AC 479 ms
18,708 KB
testcase_19 AC 92 ms
12,660 KB
testcase_20 AC 214 ms
14,292 KB
testcase_21 AC 483 ms
19,324 KB
testcase_22 AC 48 ms
11,744 KB
testcase_23 AC 236 ms
15,660 KB
testcase_24 AC 371 ms
17,844 KB
testcase_25 AC 74 ms
12,440 KB
testcase_26 AC 125 ms
13,804 KB
testcase_27 AC 248 ms
16,748 KB
testcase_28 AC 217 ms
14,476 KB
testcase_29 AC 157 ms
13,968 KB
testcase_30 AC 188 ms
14,276 KB
testcase_31 AC 434 ms
19,088 KB
testcase_32 AC 301 ms
17,644 KB
testcase_33 AC 123 ms
13,328 KB
testcase_34 AC 298 ms
17,152 KB
testcase_35 AC 338 ms
18,684 KB
testcase_36 AC 124 ms
13,468 KB
testcase_37 AC 508 ms
23,156 KB
testcase_38 AC 300 ms
17,124 KB
testcase_39 AC 219 ms
15,680 KB
testcase_40 AC 242 ms
15,180 KB
testcase_41 AC 181 ms
14,060 KB
testcase_42 AC 448 ms
18,516 KB
testcase_43 AC 245 ms
16,804 KB
testcase_44 AC 277 ms
17,148 KB
testcase_45 AC 415 ms
17,848 KB
testcase_46 AC 138 ms
13,908 KB
testcase_47 AC 208 ms
15,164 KB
testcase_48 AC 386 ms
17,800 KB
testcase_49 AC 490 ms
18,904 KB
testcase_50 AC 422 ms
18,008 KB
testcase_51 AC 225 ms
15,048 KB
testcase_52 AC 172 ms
13,996 KB
testcase_53 AC 102 ms
12,764 KB
testcase_54 AC 360 ms
19,204 KB
testcase_55 AC 309 ms
17,872 KB
testcase_56 AC 181 ms
14,096 KB
testcase_57 AC 539 ms
23,340 KB
testcase_58 AC 310 ms
16,648 KB
testcase_59 AC 176 ms
14,036 KB
testcase_60 AC 506 ms
18,832 KB
testcase_61 AC 202 ms
14,848 KB
testcase_62 AC 436 ms
18,944 KB
testcase_63 AC 334 ms
17,008 KB
testcase_64 AC 284 ms
17,572 KB
testcase_65 AC 106 ms
12,808 KB
testcase_66 AC 215 ms
14,700 KB
testcase_67 AC 172 ms
14,160 KB
testcase_68 AC 120 ms
12,612 KB
testcase_69 AC 184 ms
14,304 KB
testcase_70 AC 339 ms
17,916 KB
testcase_71 AC 239 ms
15,576 KB
testcase_72 AC 374 ms
18,260 KB
testcase_73 AC 334 ms
17,132 KB
testcase_74 AC 202 ms
14,996 KB
testcase_75 AC 272 ms
16,044 KB
testcase_76 AC 223 ms
15,424 KB
testcase_77 AC 240 ms
15,384 KB
testcase_78 AC 282 ms
15,808 KB
testcase_79 AC 413 ms
18,732 KB
testcase_80 AC 334 ms
17,744 KB
testcase_81 AC 224 ms
15,396 KB
testcase_82 AC 288 ms
15,952 KB
testcase_83 AC 317 ms
17,736 KB
testcase_84 AC 234 ms
15,476 KB
testcase_85 AC 430 ms
19,020 KB
testcase_86 AC 280 ms
16,312 KB
testcase_87 AC 377 ms
18,816 KB
testcase_88 AC 221 ms
15,180 KB
testcase_89 AC 294 ms
17,432 KB
testcase_90 AC 155 ms
15,376 KB
testcase_91 AC 154 ms
15,348 KB
testcase_92 AC 154 ms
15,520 KB
testcase_93 AC 160 ms
15,400 KB
testcase_94 AC 171 ms
15,392 KB
testcase_95 AC 692 ms
23,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;
using lint=long long;

class linear_sieve{
	vector<int> lpf,p;
public:
	linear_sieve(int n):lpf(n+1){
		for(int i=2;i<=n;i++){
			if(lpf[i]==0){
				lpf[i]=i;
				p.emplace_back(i);
			}
			for(int j=0;j<p.size()&&p[j]<=lpf[i]&&i*p[j]<=n;j++) lpf[i*p[j]]=p[j];
		}
	}

	const vector<int>& primes()const{ return p; }

	bool is_prime(int a)const{
		assert(a<=(int)lpf.size()-1);
		return a>0 && lpf[a]==a;
	}

	map<int,int> prime_factorize(int a)const{
		assert(a<=(int)lpf.size()-1);
		map<int,int> pf;
		for(;a>1;a/=lpf[a]) ++pf[lpf[a]];
		return pf;
	}

	int number_of_divisors(int a)const{
		assert(a<=(int)lpf.size()-1);
		int res=1,cnt=0,pre=-1;
		for(;a>1;a/=lpf[a]){
			if(pre==-1 || pre==lpf[a]){
				cnt++;
			}
			else{
				res*=cnt+1;
				cnt=1;
			}
			pre=lpf[a];
		}
		return res*(cnt+1);
	}
};

template<class T>
vector<T> divisors(const map<T,int>& pf){
	vector<T> res={T(1)};
	for(const auto& q:pf){
		int m=res.size();
		T pp=1;
		rep(i,q.second){
			pp*=q.first;
			rep(i,m) res.emplace_back(res[i]*pp);
		}
	}
	sort(res.begin(),res.end());
	return res;
}

template<class T>
vector<pair<T,int>> run_length_encoding(const vector<T>& a){
	vector<pair<T,int>> res;
	int n=a.size(),pre=0;
	rep(i,n) if(i==n-1 || a[i]!=a[i+1]) res.emplace_back(a[i],i-pre+1), pre=i+1;
	return res;
}

int phi[1000001];
void build_phi(){
	rep(i,1000001) phi[i]=i;
	for(int i=2;i<=1000000;i++) if(phi[i]==i) {
		for(int j=i;j<=1000000;j+=i) phi[j]=phi[j]/i*(i-1);
	}
}

int main(){
	build_phi();

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

	sort(a.begin(),a.end());

	lint ans=0;
	linear_sieve LS(1e6);
	for(auto [x,c]:run_length_encoding(a)){
		for(int d:divisors(LS.prime_factorize(x))){
			ans+=lint(c)*phi[x/d]*d;
		}
		ans-=lint(c)*x;
	}
	printf("%lld\n",ans);

	return 0;
}
0