您的位置:首页 > 其它

lightoj 1030 lightoj 1038 基础概率dp 期望

2017-07-24 11:31 537 查看
lightoj 1030


You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.

Initially you are in position 1. Now each turn you throw a perfect6 sided dice. If you get
X in the dice after throwing, you addX to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach
theNth position you stop your journey. Now you are given the information about the cave, you have to find out theexpected number of gold you can collect using the given procedure.

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

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line containsN space separated integers. The
ith integer of this line denotes the amount of gold you will get if you come to theith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than1000.

Output
For each case, print the case number and the expected number of gold you will collect. Errors less than10-6 will be ignored.

Sample Input
3

1

101

2

10 3

3

3 6 9

Sample Output
Case 1: 101.0000000000

Case 2: 13.000

Case 3: 15

题意:

给定n个格子以及每个格子上的gold值,你从第一个格子出发,每次掷1-6的骰子,根据掷出的值前进。若当前位置 + 掷出的值 > n,则重新掷骰子,到达第n个格子游戏结束。问你获得gold值的期望

题解:

可以从前往后推也可以从后往前推,见下面两份代码:

下面的代码是从后往前推的思想:

从其中的某一个点开始的期望等于后面六个点的期望值的总和加上   本身的gold * 概率(等于1,因为假设从这个点开始走)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

double a[1000],p[1000];

int main()
{
int n,T,cases=1;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf",&a[i]);
p[i]=a[i];
}

double t;
for(int i=n-1;i>=1;i--){
t=min(6,n-i);
for(int j=1;j<=6&&(i+j)<=n;j++)
p[i]+=p[i+j]/t;
}

printf("Case %d: %0.7lf\n",cases++,p[1]);
}
return 0;
}


下面这份代码是从前往后推的思想:

每一个点的概率都是由前面的点推过来的,将前面x个点的概率除以x相加即可得到当前点的概率(全概率思想)

最后求一遍期望即可

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

double a[1000],p[1000];

int main()
{
int n,T,cases=1;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf",&a[i]);
p[i]=0;
}

p[1]=1;
double t;
for(int i=1;i<=n;i++){
t=min(6,n-i);
for(int j=1;j<=6&&(i+j)<=n;j++)
p[i+j]+=p[i]/t;
}

for(int i=1;i<n;i++)
a
+=p[i]*a[i];

printf("Case %d: %0.7lf\n",cases++,a
);
}
return 0;
}


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.

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

题意:

任何一个大于1的整数, 经过若干次除以自己的因子之后可以变为1, 求变换次数的数学期望值

题解:

和上一题一样,需要提前打一个表,打表过程中就是平常判断因数的过程

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define MAXN 100000
double dp[MAXN+10];

void init()
{
dp[1]=0;
double sum,cnt,temp;
for(int i=2;i<=MAXN;i++){
sum=cnt=0;
temp=sqrt(1.0*i);
for(int j=1;j<=temp;j++){
if(i%j==0){
sum+=dp[j];
cnt++;
if(i/j!=j){
sum+=dp[i/j];
cnt++;
}
}
}
sum+=cnt;
dp[i]=sum/--cnt;
}
}

int main()
{
init();
int n,T;
int cases=1;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("Case %d: %lf\n",cases++,dp
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: