[UVA][概率DP] 11762 - Race to 1@Morris' Blog|PChome Online 人新台
2013-10-06 20:43:53| 人1,608| 回0 | 上一篇 | 下一篇

[UVA][概率DP] 11762 - Race to 1

0 收藏 0 0 站台

B

Race to 1

Input: Standard Input

Output: Standard Output

 

Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can be divided by at least one prime number less than or equal to that number. 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 prime number less than or equal to D. If D is divisible by the prime number then he divides D by the prime number to obtain new D. Otherwise he keeps the old D. He repeats this procedure until D becomes 1. What is the expected number of moves required for N to become 1.

[We say that an integer is said to be prime if its divisible by exactly two different integers. So, 1 is not a prime, by definition. List of first few primes are 2, 3, 5, 7, 11, …]

 

Input

Input will start with an integer T (T <= 1000), which indicates the number of test cases. Each of the next T lines will contain one integer N (1 <= N <= 1000000).

 

Output

For each test case output a single line giving the case number followed by the expected number of turn required. Errors up to 1e-6 will be accepted.

 

Sample Input                             Output for Sample Input

3

1

3

13

 

Case 1: 0.0000000000

Case 2: 2.0000000000

Case 3: 6.0000000000


Problemsetter: Md. Arifuzzaman Arif

Special Thanks: Sohel Hafiz

目描述:

一字 N, [2, N] 挑一 p,如果 N%p = 0,N = N/p。求次的期望值。

目解法:


很久概率 DP 了,由於有一次可能一次不。

dp[n] = p0*dp[n] + 1 + sigma(p1*dp[n/prime_factor]);


移之後便可以得到公式。

其中 p0 表示到因的率,p1 表示每因到的率。

#include <stdio.h>
#include <string.h>
#include <vector>
#define maxL (1000000>>5)+1
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5] |= 1<<(x&31))
using namespace std;
int mark[maxL];
vector<int> pfactor[1000005];
int psum[1000005] = {};
double dp[1000005];
char used[1000005] = {};
void sieve() {
    register int i, j, k;
    SET(1);
    int n = 1000000;
    for(i = 2; i <= n; i++) {
        psum[i] = psum[i-1];
        if(!GET(i)) {
            psum[i]++;
            pfactor[i].push_back(i);
            for(j = i+i; j <= n; j += i) {
                SET(j);
                pfactor[j].push_back(i);
            }
        }
    }
}//d[n]=p0*d[n]+1+p1*d[n/d1]+p1*d[n/d2]...
double dfs(int n) {
    if(n == 1)    return 0;
    if(used[n])    return dp[n];
    used[n] = 1;
    double p0, p1, e = 0;
    p0 = 1-(double)pfactor[n].size()/psum[n];
    p1 = 1.0/psum[n];
    for(int i = pfactor[n].size()-1; i >= 0; i--)
        e += dfs(n/pfactor[n][i])*p1;
    e = (e+1)/(1-p0);
    dp[n] = e;
    return e;
}
int main() {
    sieve();
    int testcase, n, cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        printf("Case %d: %.10lf\n", ++cases, dfs(n));
    }
    return 0;
}

台: Morris
人(1,608) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA][DLX] 211 - The Domino Effect
此分上一篇:[UVA][插DP] 1214 - Manhattan Wiring

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86