zoukankan      html  css  js  c++  java
  • Luogu P2567 [SCOI2010]幸运数字 容斥+脑子

    双倍经验:BZOJ 2393 Cirno的完美算数教室


    做法:先把$[1,r]$中所有的幸运数字筛出来,然后用这些幸运数字来筛$[l,r]$中的近似幸运号码;

    剪枝:当一个幸运数字$a[i]$是另一个幸运数字$a[j]$的倍数时,那么应该把$a[i]$去掉;

    贡献用容斥搞一下好了(就是dfs部分)。。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<cctype>
    #include<cstdlib>
    #include<vector>
    #include<map>
    #include<set>
    #define ll long long
    #define R register ll
    static char B[1<<15],*S=B,*D=B;
    #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
    using namespace std;
    inline ll g() {
        R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
        do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
    } ll q[5010],l,r,ans; bool vis[5010]; int tot; 
    inline void PRE(ll x) {if(x>r) return ; if(x) q[++tot]=x; PRE(x*10+6); PRE(x*10+8);}
    inline ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    inline bool cmp(const ll& a,const ll& b) {return a>b;}
    inline void dfs(int x,int d,ll L) {
        if(x==tot) return ; ++x; dfs(x,d,L); d^=1;
        if(vis[x]) return ; L=L*q[x]/gcd(L,q[x]);
        if(L<=0||L>r) return ; if(d) ans+=r/L-(l-1)/L;
        else ans-=r/L-(l-1)/L; dfs(x,d,L);
    }
    signed main() {
        l=g(),r=g(); PRE(0); sort(q+1,q+tot+1,cmp);
        for(R i=1;i<=tot;++i) for(R j=i+1;j<=tot;++j) if(q[i]%q[j]==0) {vis[i]=1; break; }
        dfs(0,0,1); printf("%lld
    ",ans);
    }

    2019.06.04别颓。

  • 相关阅读:
    读书笔记五
    读书笔记四
    读书笔记3(Teamwork)
    读书笔记二(合格的软件工程师)
    读书笔记1(软件 = 程序 + 工程)
    关于使用Java开发Mis系统
    课堂动手动脑
    Quartz学习
    把数据库中取出的DataTable转换成一个对象 或者对象列表
    SAE上使用cron定时发微博
  • 原文地址:https://www.cnblogs.com/Jackpei/p/10970988.html
Copyright © 2011-2022 走看看