結果

問題 No.2829 GCD Divination
ユーザー kotatsugamekotatsugame
提出日時 2024-08-02 21:37:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 820 bytes
コンパイル時間 939 ms
コンパイル使用メモリ 95,452 KB
実行使用メモリ 42,148 KB
最終ジャッジ日時 2024-08-02 21:37:31
合計ジャッジ時間 14,295 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34 TLE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'long double solve(int)':
main.cpp:35:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   35 |         for(auto[v,c]:nxt)ans+=solve(v)*c;
      |                 ^

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cassert>
#include<iomanip>
using namespace std;
int gcd(int a,int b)
{
	while(b)
	{
		int t=a%b;
		a=b;
		b=t;
	}
	return a;
}
map<int,long double>memo;
long double solve(int N)
{
	if(N==1)return 0;
	if(memo.find(N)!=memo.end())return memo[N];
	vector<pair<int,int> >nxt;
	{
		vector<int>to(N,1);
		for(int g=2;g<N;g++)if(N%g==0)for(int i=g;i<N;i+=g)to[i]=g;
		sort(to.begin(),to.end());
		for(int v:to)
		{
			if(!nxt.empty()&&nxt.back().first==v)nxt.back().second++;
			else nxt.push_back(make_pair(v,1));
		}
	}
	long double ans=0;
	for(auto[v,c]:nxt)ans+=solve(v)*c;
	ans+=N;
	ans/=N-1;
	return memo[N]=ans;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;cin>>N;
	cout<<fixed<<setprecision(16)<<solve(N)<<endl;
}
0