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;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11450420.html