結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 372 ms
19,008 KB
testcase_01 AC 305 ms
18,888 KB
testcase_02 AC 436 ms
18,928 KB
testcase_03 AC 295 ms
16,932 KB
testcase_04 AC 63 ms
12,052 KB
testcase_05 AC 250 ms
19,012 KB
testcase_06 AC 46 ms
11,832 KB
testcase_07 AC 75 ms
12,596 KB
testcase_08 AC 331 ms
17,600 KB
testcase_09 AC 261 ms
14,680 KB
testcase_10 AC 222 ms
14,968 KB
testcase_11 AC 354 ms
19,088 KB
testcase_12 AC 374 ms
19,356 KB
testcase_13 AC 333 ms
16,924 KB
testcase_14 AC 199 ms
14,728 KB
testcase_15 AC 168 ms
14,360 KB
testcase_16 AC 345 ms
18,604 KB
testcase_17 AC 332 ms
17,688 KB
testcase_18 AC 485 ms
19,704 KB
testcase_19 AC 91 ms
12,416 KB
testcase_20 AC 212 ms
14,332 KB
testcase_21 AC 487 ms
19,184 KB
testcase_22 AC 47 ms
11,776 KB
testcase_23 AC 237 ms
15,572 KB
testcase_24 AC 348 ms
18,888 KB
testcase_25 AC 74 ms
12,460 KB
testcase_26 AC 121 ms
13,760 KB
testcase_27 AC 240 ms
16,792 KB
testcase_28 AC 215 ms
14,660 KB
testcase_29 AC 152 ms
13,936 KB
testcase_30 AC 191 ms
14,380 KB
testcase_31 AC 438 ms
19,904 KB
testcase_32 AC 301 ms
17,936 KB
testcase_33 AC 123 ms
13,412 KB
testcase_34 AC 300 ms
17,244 KB
testcase_35 AC 335 ms
18,960 KB
testcase_36 AC 117 ms
13,436 KB
testcase_37 AC 472 ms
23,296 KB
testcase_38 AC 279 ms
17,356 KB
testcase_39 AC 202 ms
15,444 KB
testcase_40 AC 211 ms
15,064 KB
testcase_41 AC 157 ms
14,224 KB
testcase_42 AC 404 ms
18,444 KB
testcase_43 AC 217 ms
17,188 KB
testcase_44 AC 247 ms
17,852 KB
testcase_45 AC 384 ms
18,108 KB
testcase_46 AC 129 ms
13,764 KB
testcase_47 AC 183 ms
15,256 KB
testcase_48 AC 351 ms
17,960 KB
testcase_49 AC 445 ms
19,376 KB
testcase_50 AC 385 ms
19,100 KB
testcase_51 AC 193 ms
15,000 KB
testcase_52 AC 153 ms
14,032 KB
testcase_53 AC 95 ms
12,680 KB
testcase_54 AC 328 ms
19,608 KB
testcase_55 AC 290 ms
18,820 KB
testcase_56 AC 170 ms
14,328 KB
testcase_57 AC 493 ms
24,348 KB
testcase_58 AC 278 ms
16,596 KB
testcase_59 AC 157 ms
14,016 KB
testcase_60 AC 473 ms
20,168 KB
testcase_61 AC 185 ms
14,584 KB
testcase_62 AC 398 ms
19,408 KB
testcase_63 AC 309 ms
17,000 KB
testcase_64 AC 265 ms
18,996 KB
testcase_65 AC 98 ms
12,612 KB
testcase_66 AC 198 ms
14,788 KB
testcase_67 AC 158 ms
14,364 KB
testcase_68 AC 106 ms
12,904 KB
testcase_69 AC 166 ms
14,508 KB
testcase_70 AC 308 ms
18,292 KB
testcase_71 AC 223 ms
15,556 KB
testcase_72 AC 344 ms
19,292 KB
testcase_73 AC 300 ms
17,384 KB
testcase_74 AC 175 ms
15,084 KB
testcase_75 AC 248 ms
16,080 KB
testcase_76 AC 205 ms
15,456 KB
testcase_77 AC 215 ms
15,308 KB
testcase_78 AC 253 ms
15,772 KB
testcase_79 AC 376 ms
19,968 KB
testcase_80 AC 298 ms
18,376 KB
testcase_81 AC 195 ms
15,348 KB
testcase_82 AC 276 ms
16,044 KB
testcase_83 AC 304 ms
18,780 KB
testcase_84 AC 218 ms
15,584 KB
testcase_85 AC 401 ms
19,228 KB
testcase_86 AC 248 ms
16,156 KB
testcase_87 AC 346 ms
19,044 KB
testcase_88 AC 196 ms
15,028 KB
testcase_89 AC 270 ms
17,896 KB
testcase_90 AC 131 ms
15,308 KB
testcase_91 AC 134 ms
15,440 KB
testcase_92 AC 141 ms
15,416 KB
testcase_93 AC 138 ms
15,508 KB
testcase_94 AC 147 ms
15,384 KB
testcase_95 AC 665 ms
23,820 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]*((x-1)%d);
		}
	}
	printf("%lld\n",ans);

	return 0;
}
0