设dp_i为所求答案,每次选择因数的概率相同,设i有x个因数,dp_i=sum(1/x*x_j)+1,(x_j表示第j个因数),那我们就预处理每个数的因数即可,T=10000,需要预处理出答案
#include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x)&(-x)) typedef long long LL; typedef pair<int,int> pii; const int maxn = 1e5+5; vector<int> factor[maxn]; double dp[maxn]; void get_factor() { for(int i = 1; i < maxn; ++i) { for(int j = i; j < maxn; j += i) factor[j].push_back(i); } } void prework() { for(int i = 2; i < maxn; ++i) { int siz = factor[i].size(); for(int j = 0; j < siz-1; ++j) { dp[i] += 1.0/siz * dp[factor[i][j]]; } dp[i]++; dp[i] *= (double)siz/(siz-1); } } void run_case() { int n; cin >> n; cout << dp[n] << "\n"; } int main() { //ios::sync_with_stdio(false), cin.tie(0); cout.flags(ios::fixed);cout.precision(10); int t; cin >> t; //while(t--) get_factor(); prework(); for(int i = 1; i <= t; ++i) { cout << "Case " << i << ": "; run_case(); } //cout.flush(); return 0; }
来源:https://www.cnblogs.com/GRedComeT/p/12342030.html