bzoj2393 Cirno的完美算数教室

没有嘟嘟嘟,权限题。
放个题面


首先能想到\(2 ^ {10}\)枚举所有baka数,然后好像只能暴力容斥了……
没错就是容斥,不过接下来有几个重要的剪枝。
1.如果某一个baka数是另一个数的倍数,我们就筛去他。
2.容斥的时候,如果当前lcm大于R的话,及时返回。
最后剩下500个左右,然后就过了……


(此题数据范围\(1e9\)\(1e10\)确实过不了)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int N = 11;
const int maxn = 2e4 + 5;
In ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
In void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
  freopen("2393.in", "r", stdin);
  freopen("2393.out", "w", stdout);
#endif
}

int tot = 0, tot2 = 0;
ll L, R, a[maxn], b[maxn], ans = 0;
In void dfs1(ll num, int now)
{
  if(num > R || now == N) return;
  if(num) a[++tot] = num;
  dfs1(num * 10 + 2, now + 1);
  dfs1(num * 10 + 9, now + 1);
}
bool vis[maxn];
In void init()
{
  dfs1(0, 0);
  sort(a + 1, a + tot + 1);
  for(int i = 1; i <= tot; ++i)
    {
      if(vis[i]) continue;
      b[++tot2] = a[i];
      for(int j = i + 1; j <= tot; ++j)
	if(!(a[j] % a[i])) vis[j] = 1;
    }
}

In ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
In ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}

In void dfs(ll Lcm, int now, int sum, bool flg)
{
  if(flg)
    {
      ll tp = R / Lcm - (L - 1) / Lcm;
      if(sum & 1) ans += tp;
      else ans -= tp;
    }
  if(Lcm > R || now == tot2 + 1) return;
  dfs(lcm(Lcm, b[now]), now + 1, sum + 1, 1);
  dfs(Lcm, now + 1, sum, 0);
}

int main()
{
  MYFILE();
  L = read(), R = read();
  init();
  dfs(1, 1, 0, 0);
  write(ans), enter;
  return 0;
}
posted @ 2019-05-29 14:47  mrclr  阅读(236)  评论(0编辑  收藏  举报