LightOJ-1038-Race to 1 Again(概率DP)
Posted ydddd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LightOJ-1038-Race to 1 Again(概率DP)相关的知识,希望对你有一定的参考价值。
链接:
https://vjudge.net/problem/LightOJ-1038
题意:
Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D.
In each turn he randomly chooses a divisor of D (1 to D). Then he divides D by the number to obtain new D. He repeats this procedure until D becomes 1. What is the expected number of moves required for N to become 1.
思路:
概率DP,从小到大,考虑每个因子,推公式DP[i]为i所需的步数.
DP[i] = (DP[a1]+..+DP[an] + n)/n..其中n为因子个数.因为DP[an] = DP[i].经过转化得到DP[i] = (sum+n)/n-1
其中sum = DP[a1...an-1].
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
#include <assert.h>
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
double Dp[MAXN];
int n;
int main()
Dp[1] = 0;
for (int i = 2;i <= MAXN-1;i++)
double sum = 0;
int ans = 0;
for (int j = 1;j*j <= i;j++)
if (i%j == 0)
sum += Dp[j];
ans++;
if (j != i / j)
sum += Dp[i / j];
ans++;
sum += ans;
Dp[i] = sum/(ans-1);
int t, cnt = 0;
scanf("%d",&t);
while (t--)
scanf("%d", &n);
printf("Case %d: %.7lf\n", ++cnt, Dp[n]);
return 0;
以上是关于LightOJ-1038-Race to 1 Again(概率DP)的主要内容,如果未能解决你的问题,请参考以下文章
LightOJ 1038 Race To 1 Again(概率DP)
LightOJ - 1038 Race to 1 Again(期望)
LightOJ 1038 - Race to 1 Again (给一个数,用这个数的因数除以这个数,直到为1时,求除的次数的期望。)(概率)