BZOJ 1853: [Scoi2010]幸运数字

时间:2021-08-16 23:24:14

1853: [Scoi2010]幸运数字

Time Limit: 2 Sec  Memory Limit: 64 MB
Submit: 2117  Solved: 779
[Submit][Status][Discuss]

Description

在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”总是太少了,比如在[1,100]的区间内就只有6个(6,8,66,68,86,88),于是他又定义了一种“近似幸运号码”。lxhgww规定,凡是“幸运号码”的倍数都是“近似幸运号码”,当然,任何的“幸运号码”也都是“近似幸运号码”,比如12,16,666都是“近似幸运号码”。 现在lxhgww想知道在一段闭区间[a, b]内,“近似幸运号码”的个数。

Input

输入数据是一行,包括2个数字a和b

Output

输出数据是一行,包括1个数字,表示在闭区间[a, b]内“近似幸运号码”的个数

Sample Input

【样例输入1】
1 10
【样例输入2】
1234 4321

Sample Output

【样例输出1】
2
【样例输出2】
809

HINT

【数据范围】
对于30%的数据,保证1 < =a < =b < =1000000
对于100%的数据,保证1 < =a < =b < =10000000000

Source

[Submit][Status][Discuss]

分析

不难想到用容斥原理来统计。

先预处理出所有小于1e10的幸运数字,并不是很多。但是发现枚举所有的组合还是会爆炸的,需要一些剪枝。

1. 对于两个幸运数字,x<y,如果有y为x的倍数,则y可以忽略,因为x可以完全覆盖y的倍数。

2. 对于一种组合,如果目前的积已经大于N,即再进行下去得到的都是加减0的无意义操作,可以直接跳出。

3. 可以把GCD函数写成非递归的形式,但貌似没多大用,跑出来的结果差距不是很大,也许是我写得不好。

4. 枚举的时候从大往小枚举,据说有奇效,因为懒癌晚期,我并没有对比验证。

代码

 #include <bits/stdc++.h>

 using namespace std;

 typedef long long LL;

 const int N = ;

 const LL lim = 10000000000LL;

 template <class T>
__inline void read(T &x)
{
x = ; char c = getchar(); while (c < '')
c = getchar(); while (c >= '')
{
x = x* + c - '';
c = getchar();
}
} LL gcd(LL a, LL b)
{
if (a < b)
{
a ^= b;
b ^= a;
a ^= b;
}
while (b)
{
a %= b;
a ^= b;
b ^= a;
a ^= b;
}
return a;
} LL num[N]; int tot = ; __inline void prework(void)
{
int t, tail = ; for (t = ; num[t] <= lim; ++t)
{
num[++tail] = num[t] * + ;
num[++tail] = num[t] * + ;
} for (int i = ; i <= t; ++i)
{
bool flag = true;
for (int j = ; j <= tot; ++j)
if (num[i] % num[j] == )
{ flag = false; break; }
if (flag)num[++tot] = num[i];
}
} LL answer, limit; void search(int t, bool f, LL sum)
{
if (t)
{
search(t - , f, sum); LL GCD = gcd(num[t], sum); if (sum / GCD <= limit / num[t])
{
LL LCM = sum / GCD * num[t]; if (f)
answer -= limit / LCM;
else
answer += limit / LCM; search(t - , !f, LCM);
}
}
} __inline LL count(LL n)
{
limit = n;
answer = ; int pos = ; while (pos <= tot
&& num[pos] <= n)++pos; search(pos - , , ); return answer;
} signed main(void)
{
prework(); LL a; read(a);
LL b; read(b); printf("%lld\n",
count(b)
- count(a - )
);
}

BZOJ_1853.cpp

@Author: YouSiki