【bzoj2393】 Cirno的完美算数教室 容斥原理

这道题其实跟幸运数字一模一样,再写一遍,但是还是不知道为什么时间复杂度是有保证的?!!


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>

using namespace std;

long long l,r;
long long a[20010];
bool tag[20010];
long long ans;
int tot;

void dfs1(long long x)
{
	if (x>r) return;
	if (x) a[++tot]=x;
	dfs1(x*10+2);dfs1(x*10+9);
}

bool cmp(long long a,long long b)
{
	return a>b;
}

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

void dfs2(int i,long long lcm,int dep)
{
	if (i>tot) return;
	dfs2(i+1,lcm,dep);
	dep++;
	if (tag[i]) return;
	lcm=lcm*a[i]/gcd(lcm,a[i]);
	if (lcm<=0 || lcm>r) return;
	if (dep&1) ans+=r/lcm-(l-1)/lcm;
	else ans-=r/lcm-(l-1)/lcm;
	dfs2(i+1,lcm,dep);
}

int main()
{
	scanf("%lld%lld",&l,&r);
	dfs1(0);
	sort(a+1,a+tot+1,cmp);
	for (int i=1;i<=tot;i++)
	  for (int j=i+1;j<=tot;j++)
	    if (i%j==0) tag[i]=1;
	dfs2(1,1,0);
	printf("%lld\n",ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值