首先发现暴搜是2^50级别,明显T了
我们搜索的时候,剪枝一下。
如果当前最大最小值都不满足满足题意的话就不搜了,我们可以用前缀和维护这个东西
于是就6s卡过
1 /************************************************************** 2 Problem: 1224 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:6800 ms 7 Memory:804 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cmath> 12 13 using namespace std; 14 typedef double lf; 15 const lf eps = (lf) 1e-10; 16 const int N = 60; 17 18 int n, m, ans; 19 lf x, y, T; 20 lf s[N]; 21 22 void dfs(int pos, int cnt, lf now) { 23 lf Max = now + s[pos + (n - cnt) - 1] - s[pos - 1], 24 Min = now + s[m] - s[m - (n - cnt)]; 25 if (Min - T > eps || Max - T < -eps) return; 26 if (cnt == n) { 27 ++ans; 28 return; 29 } 30 if (pos == m + 1) return; 31 dfs(pos + 1, cnt, now); 32 dfs(pos + 1, cnt + 1, now + (lf) 1 / pos); 33 } 34 35 int main() { 36 int i; 37 scanf("%d%d%lf%lf", &n, &m, &x, &y); 38 T = (lf) x / y; 39 for (i = 1; i <= m; ++i) 40 s[i] = s[i - 1] + (lf) 1 / i; 41 dfs(1, 0, 0); 42 printf("%d\n", ans); 43 return 0; 44 }
(p.s. 论c++的坑爹之处:浮点数运算)
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen