結果

問題 No.1006 Share an Integer
ユーザー furafura
提出日時 2020-03-06 21:25:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,931 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 2,323 ms
コンパイル使用メモリ 211,048 KB
実行使用メモリ 19,532 KB
最終ジャッジ日時 2023-08-04 06:55:57
合計ジャッジ時間 20,493 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
11,828 KB
testcase_01 AC 17 ms
11,900 KB
testcase_02 AC 16 ms
11,600 KB
testcase_03 AC 15 ms
11,600 KB
testcase_04 AC 17 ms
11,900 KB
testcase_05 AC 17 ms
11,712 KB
testcase_06 AC 16 ms
11,900 KB
testcase_07 AC 17 ms
11,600 KB
testcase_08 AC 16 ms
11,772 KB
testcase_09 AC 17 ms
11,644 KB
testcase_10 AC 18 ms
11,600 KB
testcase_11 AC 1,041 ms
16,104 KB
testcase_12 AC 1,813 ms
18,756 KB
testcase_13 AC 1,931 ms
19,408 KB
testcase_14 AC 1,926 ms
19,456 KB
testcase_15 AC 1,637 ms
18,340 KB
testcase_16 AC 775 ms
14,776 KB
testcase_17 AC 1,074 ms
16,088 KB
testcase_18 AC 1,641 ms
18,408 KB
testcase_19 AC 1,563 ms
18,284 KB
testcase_20 AC 1,688 ms
18,476 KB
testcase_21 AC 1,919 ms
19,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

using namespace std;

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

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

const int INF=1<<29;

int main(){
	linear_sieve S(2e6);

	int n; scanf("%d",&n);
	vector<int> res(n);
	res[0]=INF;
	for(int a=1;a<n;a++){
		int fa=a-divisors(S.prime_factorize(a)).size();
		int fb=(n-a)-divisors(S.prime_factorize(n-a)).size();
		res[a]=abs(fa-fb);
	}

	int mn=*min_element(res.begin(),res.end());
	for(int a=1;a<n;a++){
		if(res[a]==mn) printf("%d %d\n",a,n-a);
	}

	return 0;
}
0