1853: [Scoi2010]幸运数字
Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1812 Solved: 663
[ 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
1 10
【样例输入2】
1234 4321
Sample Output
【样例输出1】
2
【样例输出2】
809
2
【样例输出2】
809
HINT
【数据范围】
对于30%的数据,保证1 < =a < =b < =1000000
对于100%的数据,保证1 < =a < =b < =10000000000
Source
题解:同BZOJ 2393
需要注意在求lcm的过程中可能爆long long ,计算过程需要用double
再就是需要对幸运数字从大到小排序,因为这样很快就会超过r,能减少一些不必要的搜索过程,否则可能会TLE
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 100000
#define ll long long
using namespace std;
ll l,r,a[N],b[N],vis[N],c[N];
ll ans;
int n,m,cnt;
void get_num(ll x)
{
if (x>r) return;
if (x) a[++cnt]=x;
get_num(x*10+8);
get_num(x*10+6);
}
ll gcd(ll x,ll y)
{
ll r;
while (y!=0)
{
r=x%y;
x=y;
y=r;
}
return x;
}
void dfs(int x,int y,ll lcm)
{
if (x>n)
{
if (y&1) ans+=r/lcm-(l-1)/lcm;
else if (y) ans-=r/lcm-(l-1)/lcm;
return;
}
dfs(x+1,y,lcm);
double t=(double)lcm*b[x]/(double)gcd(lcm,b[x]);
if (t<=r)
{
ll k=(ll)t;
dfs(x+1,y+1,k);
}
}
int main()
{
scanf("%lld%lld",&l,&r);
get_num(0);
sort(a+1,a+cnt+1);
for (int i=1;i<=cnt;i++)
if (!vis[i])
{
c[++n]=a[i];
for (int j=i;j<=cnt;j++)
if (!(a[j]%a[i])) vis[j]=1;
}
for (int i=1;i<=n;i++)
b[n-i+1]=c[i];
dfs(1,0,1);
printf("%lld\n",ans);
return 0;
}