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别颓。

  • 相关阅读:
    关于ArcMap中的地图文档单位
    洛谷—— P2983 [USACO10FEB]购买巧克力Chocolate Buying
    COGS——T 826. [Tyvj Feb11] GF打dota
    洛谷—— P1855 榨取kkksc03
    洛谷—— P2663 越越的组队
    COGS——T 1578. 次小生成树初级练习题
    Django中间件
    March 7 2017 Week 10 Tuesday
    March 6 2017 Week 10 Monday
    March 5 2017 Week 10 Sunday
  • 原文地址:https://www.cnblogs.com/Jackpei/p/10970988.html
Copyright © 2011-2022 走看看