結果

問題 No.1262 グラフを作ろう!
ユーザー furafura
提出日時 2020-10-17 21:30:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 592 ms / 3,000 ms
コード長 1,914 bytes
コンパイル時間 2,346 ms
コンパイル使用メモリ 216,360 KB
実行使用メモリ 24,396 KB
最終ジャッジ日時 2023-09-28 08:37:13
合計ジャッジ時間 32,014 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 349 ms
20,588 KB
testcase_01 AC 291 ms
18,152 KB
testcase_02 AC 398 ms
18,956 KB
testcase_03 AC 277 ms
17,064 KB
testcase_04 AC 59 ms
12,228 KB
testcase_05 AC 232 ms
17,292 KB
testcase_06 AC 44 ms
12,076 KB
testcase_07 AC 71 ms
12,516 KB
testcase_08 AC 300 ms
18,808 KB
testcase_09 AC 192 ms
14,680 KB
testcase_10 AC 204 ms
14,824 KB
testcase_11 AC 318 ms
17,712 KB
testcase_12 AC 331 ms
19,420 KB
testcase_13 AC 292 ms
17,040 KB
testcase_14 AC 193 ms
14,752 KB
testcase_15 AC 155 ms
14,428 KB
testcase_16 AC 319 ms
18,372 KB
testcase_17 AC 306 ms
18,148 KB
testcase_18 AC 452 ms
19,336 KB
testcase_19 AC 81 ms
12,484 KB
testcase_20 AC 184 ms
14,304 KB
testcase_21 AC 432 ms
19,972 KB
testcase_22 AC 43 ms
11,960 KB
testcase_23 AC 208 ms
15,640 KB
testcase_24 AC 324 ms
18,476 KB
testcase_25 AC 64 ms
12,460 KB
testcase_26 AC 110 ms
13,780 KB
testcase_27 AC 219 ms
17,724 KB
testcase_28 AC 204 ms
14,292 KB
testcase_29 AC 131 ms
14,064 KB
testcase_30 AC 159 ms
14,208 KB
testcase_31 AC 376 ms
20,320 KB
testcase_32 AC 257 ms
18,100 KB
testcase_33 AC 107 ms
13,220 KB
testcase_34 AC 259 ms
17,416 KB
testcase_35 AC 294 ms
20,188 KB
testcase_36 AC 108 ms
13,624 KB
testcase_37 AC 447 ms
23,804 KB
testcase_38 AC 266 ms
17,880 KB
testcase_39 AC 191 ms
15,512 KB
testcase_40 AC 207 ms
14,996 KB
testcase_41 AC 155 ms
14,104 KB
testcase_42 AC 389 ms
19,580 KB
testcase_43 AC 212 ms
17,120 KB
testcase_44 AC 244 ms
17,752 KB
testcase_45 AC 361 ms
18,752 KB
testcase_46 AC 119 ms
13,944 KB
testcase_47 AC 178 ms
15,096 KB
testcase_48 AC 337 ms
19,456 KB
testcase_49 AC 426 ms
20,064 KB
testcase_50 AC 357 ms
19,284 KB
testcase_51 AC 193 ms
15,188 KB
testcase_52 AC 141 ms
14,088 KB
testcase_53 AC 88 ms
12,632 KB
testcase_54 AC 309 ms
19,392 KB
testcase_55 AC 264 ms
18,188 KB
testcase_56 AC 158 ms
14,200 KB
testcase_57 AC 466 ms
24,396 KB
testcase_58 AC 267 ms
16,596 KB
testcase_59 AC 151 ms
14,160 KB
testcase_60 AC 433 ms
19,012 KB
testcase_61 AC 176 ms
14,704 KB
testcase_62 AC 374 ms
19,192 KB
testcase_63 AC 292 ms
17,184 KB
testcase_64 AC 246 ms
18,356 KB
testcase_65 AC 90 ms
12,932 KB
testcase_66 AC 189 ms
14,712 KB
testcase_67 AC 146 ms
14,096 KB
testcase_68 AC 99 ms
12,592 KB
testcase_69 AC 163 ms
14,376 KB
testcase_70 AC 295 ms
18,752 KB
testcase_71 AC 209 ms
15,644 KB
testcase_72 AC 323 ms
18,660 KB
testcase_73 AC 286 ms
17,148 KB
testcase_74 AC 174 ms
14,956 KB
testcase_75 AC 234 ms
15,980 KB
testcase_76 AC 195 ms
15,536 KB
testcase_77 AC 211 ms
15,496 KB
testcase_78 AC 243 ms
16,072 KB
testcase_79 AC 357 ms
19,208 KB
testcase_80 AC 289 ms
18,396 KB
testcase_81 AC 200 ms
15,324 KB
testcase_82 AC 249 ms
16,188 KB
testcase_83 AC 272 ms
17,736 KB
testcase_84 AC 205 ms
15,456 KB
testcase_85 AC 371 ms
20,272 KB
testcase_86 AC 239 ms
16,344 KB
testcase_87 AC 325 ms
18,968 KB
testcase_88 AC 191 ms
15,356 KB
testcase_89 AC 250 ms
17,780 KB
testcase_90 AC 131 ms
15,436 KB
testcase_91 AC 130 ms
15,280 KB
testcase_92 AC 131 ms
15,352 KB
testcase_93 AC 138 ms
15,276 KB
testcase_94 AC 147 ms
15,300 KB
testcase_95 AC 592 ms
23,804 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;
}

vector<int> phi;
void build_phi(int n){
	phi.resize(n+1);
	iota(phi.begin(),phi.end(),0);
	for(int i=2;i<=n;i++) if(phi[i]==i) {
		for(int j=i;j<=n;j+=i) phi[j]=phi[j]/i*(i-1);
	}
}

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 main(){
	build_phi(1e6);
	linear_sieve LS(1e6);

	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;
	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