日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LightOJ - 1038】Race to 1 Again(概率dp,数学期望) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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.

Input

Input starts with an integer?T (≤ 10000), denoting the number of test cases.

Each case begins with an integer?N (1 ≤ N ≤ 105).

Output

For each case of input you have to print the case number and the expected value. Errors less than?10-6?will be ignored.

Sample Input

3

1

2

50

Sample Output

Case 1: 0

Case 2: 2.00

Case 3: 3.0333333333

題目大意:

給個(gè)數(shù)字D,我們可以選擇1~D中可以被D整除的因子,除以D得到一個(gè)新的D,再用新D除以它的因子得到又一個(gè)新D,按次操作除到D=1時(shí)結(jié)束,求除的次數(shù)的期望值。

p[D]表示從D除到1的期望次數(shù),D的因子有cnt個(gè)(包括1和本身)

解題報(bào)告:

假設(shè)dp[n]代表從n開始退化到1的期望次數(shù),p[n]代表n的因子的集合(vector),且已經(jīng)排好序。

整理公式得

又因?yàn)?#xff08;因?yàn)橐呀?jīng)排好序了)

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1e5 + 5; int n,a[MAX]; double dp[MAX]; vector<int> vv[MAX]; void init() {for(int i = 1; i<MAX; i++) {for(int j = 1; j*j<=i; j++) {if((i%j) != 0) continue;vv[i].pb(j);if(j*j!=i) vv[i].pb(i/j);}} } void init2() {dp[1]=0;for(int i = 2; i<MAX; i++) {int up = vv[i].size();for(int j = 0; j<up; j++) {int v = vv[i][j];if(v == i) continue;dp[i] += dp[v]/(up-1);}dp[i]+=up*1.0/(up-1);} } int main() {init();init2();int t,iCase=0;cin>>t;while(t--) {scanf("%d",&n);printf("Case %d: %.8f\n",++iCase,dp[n]);}return 0 ; }

?

總結(jié)

以上是生活随笔為你收集整理的【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。