zoukankan      html  css  js  c++  java
  • bzoj1833: [ZJOI2010]count 数字计数 && codevs1359 数字计数

    bzoj1833 

    codevs1359

    这道题也是道数位dp 因为0有前导0这一说卡了很久 最后发现用所有位数减1~9的位数就okay.....orzczl大爷 其他就跟51nod那道统计1出现次数一样啦

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    LL read(){
        LL ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    LL f[15][10][10],w[15],cur=15,ans1[15],ans2[15];
    void prepare(){
        w[1]=1; for(int i=2;i<=12;i++) w[i]=w[i-1]*10;
        for(int i=0;i<=9;i++) f[1][i][i]=1;
        for(int i=2;i<=12;i++)
         for(int j=0;j<=9;j++)
          for(int k=0;k<=9;k++){
              for(int z=0;z<=9;z++)
                  f[i][j][k]+=f[i-1][z][k];
              if(j==k) f[i][j][k]+=w[i];
          }
    }
    void work1(LL n){
        int cur=12;
        if(!n){ans1[0]=1; return ;}
        while(w[cur]>n) cur--;
        LL tot=1; 
        for(int i=1;i<cur;i++) tot+=(w[i+1]-w[i])*i;
        tot+=(n-w[cur]+1)*cur;  
        LL v=n/w[cur];
        for(int i=0;i<=9;i++)
         for(int j=0;j<v;j++)
          ans1[i]+=f[cur][j][i];
        ans1[v]=ans1[v]+n%w[cur]+1;
        n=n%w[cur];
        for(int i=cur-1;i;i--){
            v=n/w[i];
            for(int j=1;j<=9;j++)
                for(int k=0;k<v;k++) ans1[j]+=f[i][k][j];
            ans1[v]=ans1[v]+n%w[i]+1;
            n=n%w[i]; 
        }
        //printf("%lld %lld
    ",tot,ans1[0]);
        for(int i=1;i<=9;i++) tot-=ans1[i]; ans1[0]=tot;
    }
    void work2(LL n){
        int cur=12;
        if(!n){ans2[0]=1; return ;}
        while(w[cur]>n) cur--;
        LL tot=1; 
        for(int i=1;i<cur;i++) tot+=(w[i+1]-w[i])*i;
        tot+=(n-w[cur]+1)*cur;
        LL v=n/w[cur];
        for(int i=1;i<=9;i++)
         for(int j=0;j<v;j++)
          ans2[i]+=f[cur][j][i];
        ans2[v]=ans2[v]+n%w[cur]+1;
        n=n%w[cur];
        for(int i=cur-1;i;i--){
            v=n/w[i];
            for(int j=1;j<=9;j++)
                for(int k=0;k<v;k++) ans2[j]+=f[i][k][j];
            if(v) ans2[v]=ans2[v]+n%w[i]+1;
            n=n%w[i]; 
        }
        //printf("%lld %lld
    ",tot,ans2[0]);
        for(int i=1;i<=9;i++) tot-=ans2[i]; ans2[0]=tot;
    }
    int main()
    {
        prepare();
        work1(read()-1); work2(read());
        for(int i=0;i<=9;i++){
            printf("%lld",ans2[i]-ans1[i]);
            if(i!=9) printf(" ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    IDE-Sublime【3】-配置Node.js开发环境
    Java-Android【2】-弹出对话框
    Node.js-中文分词【1】-node-segment
    Java-Android【1】-控制手机震动
    IDE-Sublime【2】-代码智能提示插件SublimeCodeIntel的安装
    Node.js-安装配置【1】-在Windows XP系统配置环境变量
    Node.js-部署【1】-防火墙端口的配置
    Node.js-npm【1】-常用命令
    Node.js-视图引擎【1】-Swig集成express的安装与配置
    NoSQL-Redis【2】-HDEL给我的一个惊喜
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7096068.html
Copyright © 2011-2022 走看看