• hdu4389 X mod f(x)[数位dp]


    X mod f(x)

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3020    Accepted Submission(s): 1182


    Problem Description
    Here is a function f(x):
       int f ( int x ) {
        if ( x == 0 ) return 0;
        return f ( x / 10 ) + x % 10;
       }

       Now, you want to know, in a given interval [A, B] (1 <= A <= B <= 109), how many integer x that mod f(x) equal to 0.
     
    Input
       The first line has an integer T (1 <= T <= 50), indicate the number of test cases.
       Each test case has two integers A, B.
     
    Output
       For each test case, output only one line containing the case number and an integer indicated the number of x.
     
    Sample Input
    2 1 10 11 20
     
    Sample Output
    Case 1: 10 Case 2: 3
     
    Author
    WHU
     
    Source
     
    Recommend
    zhuyuanchen520   |   We have carefully selected several similar problems for you:  4385 4383 4388 4387 4386 
     

    由于f(x)最大就是81,所以可以算对于1-81每一个数都求一下就可以

    #include<cstdio>
    #include<cstring>
    using namespace std;
    int cas,T,bits[20];
    int f[11][82][82][82];
    int dfs(int pos,int mod,int x,int sum,bool lim){
        if(!pos){return x==sum&&!mod;}
        int &res=f[pos][mod][x][sum],ans=0;
        if(!lim&&(~res)) return res;
        int up=!lim?9:bits[pos];
        for(int i=0;i<=up;i++){
            ans+=dfs(pos-1,(mod*10+i)%x,x,sum+i,lim&&i==bits[pos]);
        }
        if(!lim) res=ans;
        return ans;
    }
    int solve(int x){
        int len=0;int ans=0;
        for(;x;x/=10) bits[++len]=x%10;
        for(int fx=1;fx<=81;fx++) ans+=dfs(len,0,fx,0,1);
        return ans;
    }
    int main(){
        int l,r;
        memset(f,-1,sizeof f);
        for(scanf("%d",&T);T--;){
            scanf("%d%d",&l,&r);
            printf("Case %d: %d
    ",++cas,solve(r)-solve(l-1));
        }
        return 0;
    }
  • 相关阅读:
    HTML5之标签
    Linux常用命令(二十四)
    Linux常用命令(二十三)
    Python 定位excel表格的最后一个单元格的位置
    tornado学习
    Linux一些基础命令
    用python打造自己的SDK--使用setuptools打包安装
    Centos开放指定端口命令
    python sqlalchemy基本使用
    python rpc
  • 原文地址:https://www.cnblogs.com/shenben/p/6708043.html
Copyright © 2020-2023  润新知