zoukankan      html  css  js  c++  java
  • bzoj1853: [Scoi2010]幸运数字

    容斥原理。

    先求出所有只含6和8而且不是其他数的倍数的数。

    然后进行容斥dfs,小心爆long long。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    const int maxn = 30000 + 10;
    
    LL a,b,cnt,n;
    LL lucky[maxn];
    bool flag[maxn];
    
    
    void build(LL n) {
        if(n>b) return;
        if(n) lucky[++cnt]=n;
        build(n*10+6);
        build(n*10+8);    
    }
    
    bool cmp(LL a,LL b) {
        return a>b;    
    }
    
    void predo() {
        cnt=n=0;
        build(0);    
        sort(lucky+1,lucky+cnt+1,cmp);
        
        for(int i=1;i<=cnt;i++) {
            flag[i]=true;
            for(int j=cnt;j>i;j--) if(lucky[i]%lucky[j]==0) flag[i]=false;
        }
        
        for(int i=1;i<=cnt;i++) if(flag[i]) lucky[++n]=lucky[i];
    }
    
    LL gcd(LL a,LL b) {
        return b==0?a:gcd(b,a%b);    
    }
    
    LL dfs(int dep,LL cur) {
        //printf("%d %lld
    ",dep,cur);
        if(dep>n) return b/cur-a/cur;
        LL res=dfs(dep+1,cur),g=gcd(cur,lucky[dep]);
        if(cur/g<=b/lucky[dep]) 
            res-=dfs(dep+1,cur/g*lucky[dep]);
        return res;
    }
    
    
    int main() {
        scanf("%lld%lld",&a,&b);
        a--;
        predo();    
        printf("%lld
    ",b-a-dfs(1,1));
        return 0;
    }

  • 相关阅读:
    scgi_params
    ngin 模块及模板
    nginx常用模块
    HTML
    nginx部署网页小游戏
    nginx的server标签还有日志管理
    关于使用yum安装的nginx的主从配置文件的碎碎念
    判断所ping主机的操作系统
    CentOS 7修改主机名
    CentOS7 设置系统时间
  • 原文地址:https://www.cnblogs.com/invoid/p/5637009.html
Copyright © 2011-2022 走看看