hdu 4576 Robot | dp

本文介绍了一道经典的动态规划问题——求解在一个有n个格子的圆盘上,进行m次随机行走后,落在特定区间内的概率。通过定义状态dp[i][j]为进行第i次操作后到达第j个格子的概率,并实现相应的状态转移方程来解答此问题。

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

题意:

一个圆盘,有n个格子。一开始在1,进行m次操作,每次操作是走wi步。顺时针逆时针的概率相同。问你最后落在区间l ~ r的概率是多少。

思路:

dp

dp[i][j]:进行第i次操作后,到第j个格子的概率的大小。

然后进行相应转移即可。

*这题卡常数有够恶心的,单纯把输入数据的过程放到处理dp就能AC了。原来这样也有区别。

code:

/* **********************************************
Created Time: 2014-10-31 18:18:36
File Name   : tmp.cpp
*********************************************** */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 205;
const int MAXM = 1e6+5;
double dp[2][MAXN];

int qin()
{
    char ch = getchar();
    int res = 0;
    while(ch > '9' || ch < '0') ch = getchar();
    while(ch >= '0' && ch <= '9')
    {
        res = res*10 + ch-'0';
        ch = getchar();
    }
    return res;
}
        
int main()
{
    int n, m, l, r;
    while(scanf("%d%d%d%d", &n, &m, &l, &r) != EOF)
    {
        memset(dp, 0, sizeof(dp));
        if(n == 0)  break;
        //
        dp[0][0] = 1.0;
        const int MOD = n;
        int nn; 
        for(int i = 0;i < m; i++)
        {
            nn = qin();
            //scanf("%d", &nn);
            for(int j = 0;j < n; j++)
            {
                if(dp[0][j] == 0) continue;
                int n1 = (j+nn) % MOD;
                int n2 = (j-nn + MOD) % MOD;
                dp[1][n1] += dp[0][j] * 0.5;
                dp[1][n2] += dp[0][j] * 0.5;
            }
            for(int j = 0;j < n; j++)
            {
                dp[0][j] = dp[1][j];
                dp[1][j] = 0;
            }
        }

        double res = 0;
        for(int i = l-1; i < r; i++)
            res += dp[0][i];
        printf("%.4lf\n", res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值