結果

問題 No.2829 GCD Divination
ユーザー cho435
提出日時 2024-08-03 01:02:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,131 bytes
コンパイル時間 2,127 ms
コンパイル使用メモリ 206,564 KB
最終ジャッジ日時 2025-02-23 20:41:16
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)

template<typename T>
bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; }
template<typename T>
bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; }

struct io_setup {
	io_setup() {
		ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} io_setup;

vector<ll> dv(ll x){
	vector<ll> res;
	for(ll i=1;i*i<=x;i++){
		if(x%i==0){
			res.push_back(i);
			if(i*i!=x) res.push_back(x/i);
		}
	}
	sort(res.begin(),res.end());
	return res;
}

int main(){
	ll n;
	cin>>n;	
	map<ll,double> mp;
	auto dfs=[&](auto self,ll x) ->double {
		if(x==1) return 0;
		if(mp.count(x)) return mp[x];
		double res=x;
		{
			auto v=dv(x);
			reverse(v.begin(),v.end());
			vector<ll> ct;
			int m=v.size();
			rep(i,0,m){
				ll d=x/v.at(i);
				rep(j,0,i){
					if(v.at(j)%v.at(i)==0){
						d-=ct.at(j);
					}
				}
				ct.push_back(d);
			}
			rep(i,1,m){
				res+=self(self,v.at(i))*ct.at(i);
			}
		}
		res/=x-1;
		return mp[x]=res;
	};
	cout<<dfs(dfs,n)<<endl;
}
0