版权声明:转载请注明http://blog.youkuaiyun.com/blue_skyrim https://blog.youkuaiyun.com/blue_skyrim/article/details/5125389

本文介绍了一种解决特定概率问题的方法:在给定的环形路径中,从起点0出发到达任意目标点x所需的期望步数。通过观察递推公式与模拟实验,发现了该问题的规律,并给出了简洁高效的求解算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述 
You have been given a circle from 0 to n - 1. If you are currently at x, you will move to (x - 1) mod n or (x + 1) mod n with equal probability. Now we want to know the expected number of steps you need to reach x from 0. 
输入 
The first line contains one integer T — the number of test cases.

Each of the next T lines contains two integers n, x (0  ≤ x < n ≤ 1000) as we mention above. 
输出 
For each test case. Print a single float number — the expected number of steps you need to reach x from 0. The figure is accurate to 4 decimal places. 
示例输入 

3 2 
5 4 
10 5 
示例输出 
2.0000 
4.0000 
25.0000

史上最淫荡的递推! 
首先可以得出结论d[i]=0.5*d[i-1]+0.5*d[i+1]+1,那么就先递推吧

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#define MAXN 10000010
using namespace std;
int n;
double d[1010];
void fun()
{
    memset(d,0,sizeof(d));
    for(int t=0;t<5000;++t)
        for(int i=1;i<n;++i)
        d[i]=0.5*d[i+1]+0.5*d[i-1]+1.0;
}
int main()
{
    int t,x;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&x);
        fun();
        printf("%.4lf\n",d[x]);
    }
    return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

我这里模拟走5000步,得出来的数据能过样例,据说当时省赛就有人这样过的,但现在在山东理工上过不了,不知道是不是后台数据加强了。。。但依然可以根据这个得到规律,下面是我得出的n为10的所有期望 
50 
10 0 
0.0000 
10 1 
9.0000 
10 2 
16.0000 
10 3 
21.0000 
10 4 
24.0000 
10 5 
25.0000 
10 6 
24.0000 
10 7 
21.0000 
10 8 
16.0000 
10 9 
9.0000 
10 10 
0.0000 
规律很明显了,就是d[i]=(n-1)+(n-3)+(n-5)…..接着我就过了

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#define MAXN 10000010
using namespace std;
int n;
double d[1010];
int main()
{
    int t,x;
    scanf("%d",&t);
    while(t--)
    {
        d[0]=0;
        scanf("%d%d",&n,&x);
        if(x>n/2)
            x=n-x;
        for(int i=1;i<=n/2;++i)
            d[i]=d[i-1]+(n-(2*i-1));
        printf("%.4lf\n",d[x]);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值