题目链接:https://vjudge.net/problem/LightOJ-1038
思路:
首先要知道一个期望公式:E3 = (E1+1)/3 + (E2 + 1)/3 + (E3 +1)/3 ; 这个公式中的E3要根据题意来判断是否加入;比如这题求的是一个数被除的情况是有自己除自己的,所以总的期望还要包含自己本身的期望;又如求一个人的移动期望,如果存在不移动的可能,也就意味着有一定概率待在原地,这个时候等同于总期望包含本身的期望;
如果求 50 被除的期望,那么有公式 : E(50) = (E(1) + 1) / cnt + (E(2) + 1) / cnt + (E(5) + 1) / cnt + (E(10) + 1) / cnt + (E(25) + 1) / cnt + (E(50) + 1) / cnt;这里的cnt代表的是 50 的因数个数;只要把 左边的E(50) 移到左边,就能通过递推求解答案;因为这里样例比较多,预先求出所有的解会比较好;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
#include<string>
#include<set>
using namespace std;
//using namespace __gnu_pbds;
//------------------ 宏定义 --------------------------------
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define REP(i,n) for(int i=0;i<n;++i)
//------------------- 预备函数 ------------------------------
int read(){
int r=0,f=1;char p=getchar();
while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}
while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f;
}
int dcmp (double x, double y) {
if (fabs(x-y) < 1e-6) return 0;
if (x > y) return 1; return -1;
}
//------------------- 常量+声明 ------------------------------------------
//typedef tree<pair<long long,int>,null_type,less< pair<long long,int> >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int Maxn = 1e5+10;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
const int Mod = 1e9+7;
const double PI = acos(-1.0);
const double eps = 1e-6;
//-------------------------------------------------------------------------
// E(50) = (E(25)+1)/num + (E(2)+1)/num + (E(5)+1)/num + (E(10)+1)/num + (E(10)+1)/num + (E(1)+1)/num
double dp[Maxn];
int main (void)
{
dp[1] = 0;
for (int i = 2; i <= 100000; ++i) {
int cnt = 0;
for (int j = 1; j*j <= i; ++j) {
if (i % j == 0) {
if (j*j == i) {
cnt++; dp[i] += dp[j]; // cnt 记录当前 i 的因数个数
} else {
cnt += 2; dp[i] += (dp[j]+dp[i/j]);
}
}
}
dp[i] = (dp[i]+cnt)/(cnt-1);
}
int t, N;
scanf ("%d", &t);
for (int cas = 1; cas <= t; ++cas) {
scanf ("%d", &N);
if (N == 1) printf ("Case %d: %.6f\n", cas, dp[1]);
else printf ("Case %d: %.6f\n", cas, dp[N]);
}
return 0;
}