您的位置:首页 > 其它

BZOJ1853: [Scoi2010]幸运数字

2014-09-17 13:27 211 查看

1853: [Scoi2010]幸运数字

Time Limit: 2 Sec Memory Limit: 64 MB
Submit: 1129 Solved: 392
[Submit][Status]

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

Day1

题解:

原来看着好难,现在看着好简单。。。可是为什么还是T成翔啊。。。无奈看了题解。。。
http://z55250825.blog.163.com/blog/static/150230809201432103111474/
他的博客里讲的很清楚。。。

最后一个优化貌似只需要改成无符号 ll 就能过,我看的lyd的代码23333.

代码:

#include<cstdio>

#include<cstdlib>

#include<cmath>

#include<cstring>

#include<algorithm>

#include<iostream>

#include<vector>

#include<map>

#include<set>

#include<queue>

#include<string>

#define inf 1000000000

#define maxn 5000

#define maxm 500+100

#define eps 1e-10

#define ll unsigned long long

#define pa pair<int,int>

#define for0(i,n) for(int i=0;i<=(n);i++)

#define for1(i,n) for(int i=1;i<=(n);i++)

#define for2(i,x,y) for(int i=(x);i<=(y);i++)

#define for3(i,x,y) for(int i=(x);i>=(y);i--)

#define mod 1000000007

using namespace std;

inline ll read()

{

ll x=0,f=1;char ch=getchar();

while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}

return x*f;

}
ll l,r,ans,tot,n,a[maxn],b[maxn];
ll v[maxn];
inline void dfs(ll x)
{
if(x>r)return;
if(x)a[++tot]=x;
dfs(10*x+6);
dfs(10*x+8);
}
inline ll gcd(ll x,ll y)
{
return y?gcd(y,x%y):x;
}
inline void calc(ll x,int y,int z)
{
if(y>n)
{
if(z&1)ans+=r/x-(l-1)/x;
else if(z)ans-=r/x-(l-1)/x;
return;
}
calc(x,y+1,z);
ll t=(x/gcd(x,a[y]))*a[y];
if(t<=r)calc(t,y+1,z+1);
}

int main()

{

freopen("input.txt","r",stdin);

freopen("output.txt","w",stdout);

l=read();r=read();
dfs(0);
sort(a+1,a+tot+1);
for1(i,tot)
if(!v[i])
{
b[++n]=a[i];
for2(j,i+1,tot)if(a[j]%a[i]==0)v[j]=1;
}
for1(i,n)a[n+1-i]=b[i];
calc(1,1,0);
printf("%lld\n",ans);

return 0;

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: