从考虑第一步开始
p(x):代表从x到1的期望
n的所有因子为n,a1,a2....,1(可以打表求出),总个数为size, x = size - 2;
p(n) = (p(a1)+1+p(a2)+1+.....+p(ax)+1)*(1/size) + 1/size + (p(n)+1)*(1/size);
化简式子得到:
p(a1)+p(a2)+,,,+p(ax) = s;
p(n) = (s + size)/(size-1);
利用打表得到的size,少了1所以+1
/******************************************** Author :Crystal Created Time : File Name : ********************************************/ #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <climits> #include <string> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <sstream> #include <cctype> using namespace std; typedef long long ll; typedef pair<int ,int> pii; #define MEM(a,b) memset(a,b,sizeof a) #define CLR(a) memset(a,0,sizeof a); const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; //#define LOCAL double dp[200006]; std::vector<int> v[100005]; void init(){ for(int i=2;i<=100001;i++){ for(int j=i;j<=100001;j+=i){ v[j].push_back(i); } } dp[1] = 0; for(int i=2;i<=100001;i++){ double s = 0; for(int j=0;j<v[i].size();j++){ s += dp[i/v[i][j]]; } dp[i] = (s+1)/v[i].size()+1; //递推公式, } } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif init(); int t;cin >> t; int kase = 1; while(t--){ int n;cin >>n; printf("Case %d: %.10lf\n",kase++,dp[n]); } return 0; }