2393: Cirno的完美算数教室
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 557 Solved: 337
[Submit][Status][Discuss]
Description
~Cirno发现了一种baka数,这种数呢~只含有2和⑨两种数字~~
现在Cirno想知道~一个区间中~~有多少个数能被baka数整除~
但是Cirno这么天才的妖精才不屑去数啦
只能依靠聪明的你咯。
Input
一行正整数L R
( 1 < L < R < 10^10)
Output
一个正整数,代表所求的答案
Sample Input
1 100
Sample Output
58
和1853一模一样啊
最后不能被其他baka数整除的baka数很少
用容斥原理,直接硬搞
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=2010;
ll l,r;
int tot,n,ans;
ll a[N];
bool book[N];
void getnum(ll x)
{
if(x>r)return ;
if(x)a[++tot]=x;
getnum(x*10+2);getnum(10*x+9);
}
ll gcd(ll a,ll b)
{return b?gcd(b,a%b):a;}
void dfs(int x,int y,int z)
{
if(!x)
{
if(y&1)ans+=r/z-(l-1)/z;
else if(y)ans-=r/z-(l-1)/z;
return ;
}
dfs(x-1,y,z);
ll t=z/gcd(z,a[x]);
if((double)t*a[x]<=r)dfs(x-1,y+1,t*a[x]);
}
int main()
{
l=read();r=read();
getnum(0);
sort(a+1,a+1+tot);
register int i,j;
for(i=1;i<=tot;++i)if(!book[i])
{
a[++n]=a[i];
for(j=1;j<=tot;++j)if(a[j]%a[i]==0)book[j]=1;
}
dfs(tot,0,1);
cout<<ans<<endl;
}
/*
1 100
58
*/