【bzoj2393】Cirno的完美算数教室
Description
~Cirno发现了一种baka数,这种数呢~只含有2和⑨两种数字~~
现在Cirno想知道~一个区间中~~有多少个数能被baka数整除~
但是Cirno这么天才的妖精才不屑去数啦
只能依靠聪明的你咯。
Input
一行正整数L R
( 1 < L < R < 10^10)
Output
一个正整数,代表所求的答案
Sample Input
1 100
Sample Output
58
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#define N 100000
using namespace std;
typedef long long LL;
int a[N];
int b[N];
bool vis[100];
int l,r;
int n,t;
int m;
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
void dfs(int x,int y) //全排列
{
if(x>t||y>r) return;
dfs(x+1,10*y+2);
a[n++]=y;
dfs(x+1,10*y+9);
a[n++]=y;
}
int solve() //容斥
{
int sum=0;
for(int i=1;i<(1<<m);i++)
{
LL mul=1;
int bits=0;
for(int j=0;j<m;j++)
{
if((1<<j)&i)
{
bits++;
mul=mul*b[j]/gcd(mul,b[j]);
}
}
cout<<"%%%"<<mul<<endl;
if(bits&1)
sum+=r/mul+(l-1)/mul;
else
sum-=r/mul+(l-1)/mul;
}
return sum;
}
int main()
{
while(cin>>l>>r)
{
memset(vis,false,sizeof vis);
t=int(log(r)/log(10))+1;
cout<<"--"<<t<<endl;
n=0;
m=0;
//int m=0;
dfs(0,0);
for(int i=0;i<n;i++) //筛法
if(!vis[i])
{
b[m++]=a[i];
for(int j=i+1;j<n;j++)
{
//c//out<<j<<endl;
//system("pause");
if(!(a[j]%a[i]))
vis[j]=1;
}
}
for(int i=0;i<m;i++)
cout<<"&"<<b[i]<<endl;
cout<<solve()<<endl;
}
return 0;
}