bzoj 2393(容斥原理)

来源:互联网 发布:github客户端 for mac 编辑:程序博客网 时间:2024/05/04 15:36

2393: Cirno的完美算数教室

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 394  Solved: 231
[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


解题思路:先可以枚举出baka数,然后再删掉两两可以整除的。

然后在dfs,加点剪枝,就是当lcm>r时直接退出。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long l,r,len,cnt,ans;
long long q[2000],g[2000];
bool b[2000];


inline long long read()
{
char y; long long x=0,f=1; y=getchar();
while (y<'0' || y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0' && y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;



long long gcd(long long a,long long b)
 {
  if (b==0) return a;
 else return gcd(b,a%b);
 }


void dfs(long long sum)
 {
  if (sum>r) return;
if (sum!=0)++len,q[len]=sum;
  dfs(sum*10+2);
  dfs(sum*10+9);
 }


void work(long long sum,int ge,int now)
 {
  if (sum>r) return;
  if (now==cnt+1)
  {
   if (sum!=1) {if (ge%2==1)ans+=r/sum-(l-1)/sum;else ans-=r/sum-(l-1)/sum;}
   return;
}
    long long ogg=gcd(sum,g[now]);
    work(sum*g[now]/ogg,ge+1,now+1);
    work(sum,ge,now+1);
 }


int main()
{
l=read(); r=read();
dfs(0);
memset(b,true,sizeof(b));
for (int i=1;i<=len;++i)
for (int j=1;j<=len;++j)
 if (i!=j && q[i]%q[j]==0) b[i]=false;
for (int i=1;i<=len;++i)
if (b[i])
 {
  ++cnt; g[cnt]=q[i];
 }
ans=0;
work(1,0,1);
printf("%lld",ans);
}

0 0
原创粉丝点击