結果

問題 No.1262 グラフを作ろう!
ユーザー furafura
提出日時 2020-10-16 23:22:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 673 ms / 3,000 ms
コード長 1,888 bytes
コンパイル時間 2,909 ms
コンパイル使用メモリ 216,272 KB
実行使用メモリ 24,564 KB
最終ジャッジ日時 2023-09-28 05:24:05
合計ジャッジ時間 37,519 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 374 ms
20,520 KB
testcase_01 AC 313 ms
17,908 KB
testcase_02 AC 452 ms
19,716 KB
testcase_03 AC 315 ms
16,824 KB
testcase_04 AC 65 ms
12,168 KB
testcase_05 AC 256 ms
17,868 KB
testcase_06 AC 47 ms
11,864 KB
testcase_07 AC 77 ms
12,680 KB
testcase_08 AC 330 ms
19,408 KB
testcase_09 AC 222 ms
14,784 KB
testcase_10 AC 242 ms
14,888 KB
testcase_11 AC 343 ms
17,668 KB
testcase_12 AC 354 ms
19,772 KB
testcase_13 AC 351 ms
17,016 KB
testcase_14 AC 215 ms
14,732 KB
testcase_15 AC 174 ms
14,384 KB
testcase_16 AC 369 ms
18,112 KB
testcase_17 AC 352 ms
18,664 KB
testcase_18 AC 510 ms
19,800 KB
testcase_19 AC 89 ms
12,612 KB
testcase_20 AC 232 ms
14,248 KB
testcase_21 AC 520 ms
21,108 KB
testcase_22 AC 51 ms
11,732 KB
testcase_23 AC 247 ms
15,540 KB
testcase_24 AC 370 ms
19,208 KB
testcase_25 AC 78 ms
12,524 KB
testcase_26 AC 133 ms
13,800 KB
testcase_27 AC 261 ms
18,416 KB
testcase_28 AC 229 ms
14,304 KB
testcase_29 AC 161 ms
14,016 KB
testcase_30 AC 199 ms
14,276 KB
testcase_31 AC 455 ms
20,088 KB
testcase_32 AC 315 ms
19,012 KB
testcase_33 AC 114 ms
13,456 KB
testcase_34 AC 298 ms
17,524 KB
testcase_35 AC 350 ms
19,612 KB
testcase_36 AC 115 ms
13,592 KB
testcase_37 AC 533 ms
23,528 KB
testcase_38 AC 283 ms
17,640 KB
testcase_39 AC 229 ms
15,556 KB
testcase_40 AC 223 ms
15,156 KB
testcase_41 AC 194 ms
14,148 KB
testcase_42 AC 472 ms
18,440 KB
testcase_43 AC 258 ms
17,228 KB
testcase_44 AC 296 ms
18,200 KB
testcase_45 AC 441 ms
18,772 KB
testcase_46 AC 150 ms
13,872 KB
testcase_47 AC 212 ms
15,120 KB
testcase_48 AC 364 ms
18,772 KB
testcase_49 AC 464 ms
19,964 KB
testcase_50 AC 400 ms
18,512 KB
testcase_51 AC 206 ms
15,160 KB
testcase_52 AC 154 ms
14,028 KB
testcase_53 AC 98 ms
12,860 KB
testcase_54 AC 341 ms
19,360 KB
testcase_55 AC 324 ms
18,336 KB
testcase_56 AC 170 ms
14,132 KB
testcase_57 AC 521 ms
23,188 KB
testcase_58 AC 290 ms
16,680 KB
testcase_59 AC 165 ms
14,152 KB
testcase_60 AC 471 ms
20,428 KB
testcase_61 AC 190 ms
14,660 KB
testcase_62 AC 435 ms
19,752 KB
testcase_63 AC 311 ms
17,132 KB
testcase_64 AC 267 ms
18,344 KB
testcase_65 AC 98 ms
12,736 KB
testcase_66 AC 198 ms
14,772 KB
testcase_67 AC 162 ms
14,108 KB
testcase_68 AC 106 ms
12,672 KB
testcase_69 AC 169 ms
14,244 KB
testcase_70 AC 315 ms
18,140 KB
testcase_71 AC 220 ms
15,652 KB
testcase_72 AC 352 ms
18,956 KB
testcase_73 AC 301 ms
17,196 KB
testcase_74 AC 185 ms
15,004 KB
testcase_75 AC 251 ms
16,112 KB
testcase_76 AC 213 ms
15,352 KB
testcase_77 AC 219 ms
15,416 KB
testcase_78 AC 260 ms
15,940 KB
testcase_79 AC 383 ms
19,388 KB
testcase_80 AC 312 ms
18,904 KB
testcase_81 AC 212 ms
15,436 KB
testcase_82 AC 274 ms
16,020 KB
testcase_83 AC 296 ms
18,316 KB
testcase_84 AC 215 ms
15,472 KB
testcase_85 AC 415 ms
20,672 KB
testcase_86 AC 258 ms
16,224 KB
testcase_87 AC 352 ms
19,392 KB
testcase_88 AC 205 ms
15,136 KB
testcase_89 AC 271 ms
19,152 KB
testcase_90 AC 146 ms
15,276 KB
testcase_91 AC 142 ms
15,400 KB
testcase_92 AC 136 ms
15,356 KB
testcase_93 AC 146 ms
15,452 KB
testcase_94 AC 160 ms
15,520 KB
testcase_95 AC 673 ms
24,564 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,m; scanf("%d%d",&n,&m);
	vector<int> a(m);
	rep(i,m) 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