BZOJ1853: [Scoi2010]幸运数字

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.

代码:

  1 #include<cstdio>
  2 
  3 #include<cstdlib>
  4 
  5 #include<cmath>
  6 
  7 #include<cstring>
  8 
  9 #include<algorithm>
 10 
 11 #include<iostream>
 12 
 13 #include<vector>
 14 
 15 #include<map>
 16 
 17 #include<set>
 18 
 19 #include<queue>
 20 
 21 #include<string>
 22 
 23 #define inf 1000000000
 24 
 25 #define maxn 5000
 26 
 27 #define maxm 500+100
 28 
 29 #define eps 1e-10
 30 
 31 #define ll unsigned long long
 32 
 33 #define pa pair<int,int>
 34 
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36 
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38 
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40 
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42 
 43 #define mod 1000000007
 44 
 45 using namespace std;
 46 
 47 inline ll read()
 48 
 49 {
 50 
 51     ll x=0,f=1;char ch=getchar();
 52 
 53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 54 
 55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 56 
 57     return x*f;
 58 
 59 }
 60 ll l,r,ans,tot,n,a[maxn],b[maxn];
 61 ll v[maxn];
 62 inline void dfs(ll x)
 63 {
 64     if(x>r)return;
 65     if(x)a[++tot]=x;
 66     dfs(10*x+6);
 67     dfs(10*x+8);
 68 }    
 69 inline ll gcd(ll x,ll y)
 70 {
 71     return y?gcd(y,x%y):x;
 72 }
 73 inline void calc(ll x,int y,int z)
 74 {
 75     if(y>n)
 76     {
 77             if(z&1)ans+=r/x-(l-1)/x;
 78             else if(z)ans-=r/x-(l-1)/x;
 79             return;    
 80     }
 81     calc(x,y+1,z);
 82     ll t=(x/gcd(x,a[y]))*a[y];
 83     if(t<=r)calc(t,y+1,z+1);
 84 }    
 85 
 86 int main()
 87 
 88 {
 89 
 90     freopen("input.txt","r",stdin);
 91 
 92     freopen("output.txt","w",stdout);
 93 
 94     l=read();r=read();
 95     dfs(0);
 96     sort(a+1,a+tot+1);
 97     for1(i,tot)
 98     if(!v[i])
 99     {
100         b[++n]=a[i];
101         for2(j,i+1,tot)if(a[j]%a[i]==0)v[j]=1;
102     }
103     for1(i,n)a[n+1-i]=b[i];
104     calc(1,1,0);
105     printf("%lld
",ans);
106 
107     return 0;
108 
109 }
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/3976917.html