BZOJ 2393: Cirno的完美算数教室 容斥原理

2393: Cirno的完美算数教室

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 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
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值