Robot HDU - 4576

Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. 



At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the robot. A command will make the robot walk some distance. Unfortunately the direction part on the remote control is broken, so for every command the robot will chose a direction(clockwise or anticlockwise) randomly with equal possibility, and then walk w cells forward. 
Michael wants to know the possibility of the robot stopping in the cell that cell number >= l and <= r after m commands. 

InputThere are multiple test cases. 
Each test case contains several lines. 
The first line contains four integers: above mentioned n(1≤n≤200) ,m(0≤m≤1,000,000),l,r(1≤l≤r≤n). 
Then m lines follow, each representing a command. A command is a integer w(1≤w≤100) representing the cell length the robot will walk for this command.   
The input end with n=0,m=0,l=0,r=0. You should not process this test case. 
OutputFor each test case in the input, you should output a line with the expected possibility. Output should be round to 4 digits after decimal points.Sample Input

3 1 1 2
1
5 2 4 4
1
2
0 0 0 0

Sample Output

0.5000
0.2500
这题目神了,把
int n, m, l, r;丢main()里就AC,丢外面就TLE,

把变量定义在主函数之外和定义在主函数之内的区别不是只在于作用域和生存周期么?想不通。。。希望路过大佬留言指点orz

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 4e18+10;
const int mod = 1000000007;
const int mx = 1e6; //check the limits, dummy
typedef pair<int, int> pa;
const double PI = acos(-1);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
#define swa(a,b) a^=b^=a^=b
#define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define rb(i,a,b) for(int i=(b),_=(a);i>=_;i--)
#define clr(a) memset(a, 0, sizeof(a))
#define lowbit(x) ((x)&(x-1))
#define mkp make_pair
void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
double dp[2][250];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m, l, r;
    while (~scanf("%d%d%d%d",&n,&m,&l,&r)){
        if (n == 0 && m == 0 && l == 0 && r == 0)break;
        dp[0][0] = 1;
        for (int i = 1; i < n; i++)dp[0][i] = 0;
        int now = 0;
        while (m--)
        {
            int v;
            scanf("%d", &v);
            for (int i = 0; i < n; i++)
                dp[now ^ 1][i] = 0.5 * dp[now][(i - v + n) % n] + 0.5 * dp[now][(i + v) % n];
            now ^= 1;
        }
        double ans = 0;
        for (int i = l - 1; i < r; i++)
            ans += dp[now][i];
        printf("%.4f\n", ans);
    }
    return 0;
}

 

posted @ 2020-04-19 11:52  XXXSANS  阅读(189)  评论(0)    收藏  举报