lightoj 1030 lightoj 1038 基础概率dp 期望

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[n]+=p[i]*a[i];

        printf("Case %d: %0.7lf\n",cases++,a[n]);
    }
    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[n]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值