HDU 4576 简单概率 + 滚动数组DP(大坑)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576

坑大发了,居然加 % 也会超时;

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(int i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define sfl(n) scanf("%I64d", &n)
21 #define pfi(n) printf("%d\n", n)
22 #define pfl(n) printf("%I64d\n", n)
23 #define MAXN 1000005
24 int n, l, m, r, d;
25 int t = 0, mm;
26 double dp[2][205];
27 int main()
28 {
29     while(~scanf("%d%d%d%d", &n, &m, &l, &r) && (n + m + l + r))
30     {
31         t = 0;
32         repu(i, 0, n + 1) dp[0][i] = 0.0;
33         dp[0][1] = 1.0;
34         int c;
35         repu(i, 0, m)
36         {
37             sfi(d);
38             if(l == 1 && r == n) continue;
39             c = d / n;
40             mm = d - n * c;
41             if(mm)
42             {
43                 t = !t;
44                 repu(j, 1, n + 1)
45                 {
46                     int lt, rt;
47                     if((n + (j - mm)) >= n) lt = (n + (j - mm)) - n;
48                     else lt = (n + (j - mm));
49                     if((j + mm) >= n) rt = (j + mm) - n;
50                     else rt = (j + mm);
51                     if(lt == 0) lt = n;
52                     if(rt == 0) rt = n;
53                     dp[t][j] = (dp[!t][lt] + dp[!t][rt]) / 2.0;
54                     //cout<<j<<" :"<<dp[t][j]<<endl;
55                 }
56             }
57         }
58         double ans = 0.0;
59         if(l == 1 && r == n) ans = 1.0;
60         else
61         repu(i, l, r + 1) ans += dp[t][i];
62         printf("%.4lf\n", ans);
63     }
64     return 0;
65 }
AC
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(int i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define sfl(n) scanf("%I64d", &n)
21 #define pfi(n) printf("%d\n", n)
22 #define pfl(n) printf("%I64d\n", n)
23 #define MAXN 1000005
24 int n, l, m, r, d;
25 int t = 0, mm;
26 double dp[2][205];
27 int main()
28 {
29     while(~scanf("%d%d%d%d", &n, &m, &l, &r) && (n + m + l + r))
30     {
31         t = 0;
32         repu(i, 0, n + 1) dp[0][i] = 0.0;
33         dp[0][1] = 1.0;
34         int c;
35         repu(i, 0, m)
36         {
37             sfi(d);
38             if(l == 1 && r == n) continue;
39             c = d / n;
40             mm = d - n * c;
41             if(mm)
42             {
43                 t = !t;
44                 repu(j, 1, n + 1)
45                 {
46                     int lt, rt;
47                     lt = (n + (j - mm)) % n;
48                     rt = (j + mm) % n;
49 //                    if((n + (j - mm)) >= n) lt = (n + (j - mm)) - n;
50 //                    else lt = (n + (j - mm));
51 //                    if((j + mm) >= n) rt = (j + mm) - n;
52 //                    else rt = (j + mm);
53                     if(lt == 0) lt = n;
54                     if(rt == 0) rt = n;
55                     dp[t][j] = (dp[!t][lt] + dp[!t][rt]) / 2.0;
56                     //cout<<j<<" :"<<dp[t][j]<<endl;
57                 }
58             }
59         }
60         double ans = 0.0;
61         if(l == 1 && r == n) ans = 1.0;
62         else
63         repu(i, l, r + 1) ans += dp[t][i];
64         printf("%.4lf\n", ans);
65     }
66     return 0;
67 }
TLE

 % 慎用啊必须,虽然不知道为什么

posted @ 2015-08-06 11:53  一麻袋码的玛侬  阅读(205)  评论(0编辑  收藏  举报