码迷,mamicode.com
首页 > 其他好文 > 详细

[BZOJ 1853] 大包子的幸运数字 容斥原理 搜索

时间:2017-08-19 22:29:53      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:using   type   i++   style   logs   scanf   lib   ==   space   

  搜索策略之: 尽快结束搜索.

  本题将数字从大到小排序.

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cctype>
 5 #include <algorithm>
 6 using namespace std;
 7 #define F(i, a, b) for (register int i = (a); i <= (b); i++)
 8 #define LL unsigned long long
 9 
10 const int N = 3000;
11 
12 LL n, m, a[N], tot, b[N]; bool v[N];
13 
14 void List(LL x) {
15     if (x > m) return;
16     if (x > 0) a[++tot] = x;
17     List(x * 10 + 6), List(x * 10 + 8);
18 }
19 
20 inline LL gcd(LL x, LL y) { return !y ? x : gcd(y, x % y); }
21 inline LL LCM(LL x, LL y) { return x / gcd(x, y) * y; }
22 LL Calc(int dep, LL x, LL Lim, int sign) {
23     if (x > Lim) return 0;
24     if (dep == tot+1) return x == 1 ? 0 : sign * (Lim / x);
25     LL tA = Calc(dep+1, x, Lim, sign);
26     return tA + Calc(dep+1, LCM(x, b[dep]), Lim, -sign);
27 }
28 
29 int main(void) {
30     #ifndef ONLINE_JUDGE
31         freopen("bzoj1853.in", "r", stdin);
32     #endif
33     
34     scanf("%lld %lld", &n, &m);
35     
36     List(0);
37     sort(a+1, a+tot+1);
38     
39     int tmp = 0;
40     F(i, 1, tot) if (!v[i]) {
41         b[++tmp] = a[i];
42         F(j, i+1, tot)
43             v[j] |= (a[j] % a[i] == 0);
44     }
45     tot = tmp;
46     reverse(b+1, b+tot+1);
47     
48     LL tR = Calc(1, 1LL, m, -1);
49     printf("%lld\n", tR - Calc(1, 1LL, n-1, -1));
50     
51     return 0;
52 }

 

[BZOJ 1853] 大包子的幸运数字 容斥原理 搜索

标签:using   type   i++   style   logs   scanf   lib   ==   space   

原文地址:http://www.cnblogs.com/Sdchr/p/7397837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!