LightOJ-1038-Race to 1 Again (probability DP)

link:

https://vjudge.net/problem/LightOJ-1038

Meaning of the questions:

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.

Ideas:

Probability DP, from small to large, consider each factor, push formula DP [i] is the number of steps required I.
DP [i] = (the DP [A1] + .. + the DP [AN] + n-) / n-. wherein n is a number of factors. since DP [an] = DP [i ]. after converted to DP [i] = (sum + n) / n-1
where sum = DP [a1 ... an- 1].

Code:

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

Guess you like

Origin www.cnblogs.com/YDDDD/p/11450420.html