因为是权限题所以把题目粘过来
2393: Cirno的完美算数教室
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 108 Solved: 62
[ Submit][ Status]
Description
~Cirno
发现了一种
baka
数,这种数呢
~
只含有
2
和⑨两种数字
~~
现在
Cirno
想知道
~
一个区间中
~~
有多少个数能被
baka
数整除
~
但是
Cirno
这么天才的妖精才不屑去数啦
只能依靠聪明的你咯。
Input
一行正整数
L R
( 1 < L < R < 10^10)
Output
一个正整数,代表所求的答案
Sample Input
1 100
Sample Output
58
1到r中有多少个x,满足x是y的约数,y只含2与9(范围10^9)
本提用了容斥原理
10^9中只含2和9的质数大概只有30个不到,那么我们只需要求出任意两个集合的并,用容斥原理的公式即偶减去奇加上。
然后爆搜即可,加上减枝:如果两个数的lcm大于r就continue
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define MAX 2100
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define ll long long
using namespace std;
int t,vis[MAX];
ll n,m,a[MAX],b[MAX],ans=0,l,r;
ll gcd(ll a1,ll a2)
{
return a2?gcd(a2,a1%a2):a1;
}
void prework(ll x,ll y)
{
if(y>r)
return;
if(x>1)
a[++m]=y;
if(x>t)
return;
prework(x+1,y*10+2);
prework(x+1,y*10+9);
}
void dfs(ll x,ll y,ll z)
{
if(x>n)
{
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 next=a[x]*z/gcd(a[x],z);
if(next<=r)
dfs(x+1,y+1,next);
return;
}
int main()
{
cin>>l>>r;
t=(int)(log(r*1.0)/log(10))+1;
prework(1,0);
sort(a+1,a+1+m);
rep(i,1,m)
if(!vis[i])
{
b[++n]=a[i];
rep(j,i+1,m)
if(!a[j]%a[i])
vis[j]=1;
}
rep(i,1,n)
a[n-i+1]=b[i];
dfs(1,0,1);
printf("%lld\n",ans);
return 0;
}