zoukankan      html  css  js  c++  java
  • [swustoj 1097] 2014

    2014(1097)

    问题描述

    今年是2014年,所以小明喜欢2014的每一位数字(即:2,0,1,4),小明想知道在区间[l,r](包括l和r)中有多少个数中含有这4个数字(数字无前缀零)。

    输入

    多组数据。

    每组数据输入2个数l,r(0<l<r<=10^9)

    输出

    输出占一行,即区间[l,r](包括l和r)中包含的满足条件的数的个数

    样例输入

    1 10
    100 1024

    样例输出

    0

    1

    简单数位DP

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    
    int bit[20];
    int dp[20][2][2][2][2];
    
    int dfs(int pos,int n2,int n0,int n1,int n4,bool limit,bool fzero)
    {
        if(pos==-1)
        {
            return n2&n0&n1&n4;
        }
        if(!limit && !fzero && dp[pos][n2][n0][n1][n4]!=-1) return dp[pos][n2][n0][n1][n4];
        int end=limit?bit[pos]:9;
        int ans=0;
        for(int i=0;i<=end;i++)
        {
            int nn2=i==2?1:0;
            int nn0=i==0?1:0;
            int nn1=i==1?1:0;
            int nn4=i==4?1:0;
            if(fzero) nn0=0;
            ans+=dfs(pos-1,n2|nn2,n0|nn0,n1|nn1,n4|nn4,limit && i==end,fzero && !i);
        }
        if(!limit && !fzero) dp[pos][n2][n0][n1][n4]=ans;
        return ans;
    }
    int cal(int n)
    {
        int len=0;
        while(n)
        {
            bit[len++]=n%10;
            n/=10;
        }
        return dfs(len-1,0,0,0,0,1,1);
    }
    int main()
    {
        int l,r;
        memset(dp,-1,sizeof(dp));
        while(cin>>l>>r)
        {
            cout<<cal(r)-cal(l-1)<<"
    ";
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    题解-FJOI2014 树的重心
    题解-CF1307G Cow and Exercise
    题解-SHOI2005 树的双中心

    【转载】SVN使用教程总结
    Fastcgi、CGI 是什么
    通过js或jq增加的代码,点击事件或其他一些事件不起作用时
    js闭包讲解
    PHP 程序员危机(转载)
    浏览器 User-Agent相关知识
  • 原文地址:https://www.cnblogs.com/hate13/p/4486380.html
Copyright © 2011-2022 走看看