zoukankan      html  css  js  c++  java
  • X mod f(x) (思维数位dp)

    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 <= 10 9), 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

    本题不容易想到的是运用枚举进行对所要凑成的数进行枚举

    这样就可以在数位dp的过程中进行取余操作

    dp数组记录的状态分别是数位 枚举值,当前数位值,余数;

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int dp[11][90][90][90];//数位 枚举值,当前数位值,余数;
    int a[11];
    int dfs(int pos,int lim, int k,int sum,int mod)
    {
        //cout<<pos<<" "<<k<<" "<<sum << " "<<mod<<endl;
        if(pos==0)
        {
            return sum==k&&mod==0;
        }
        if(!lim&&dp[pos][k][sum][mod]!=-1) return dp[pos][k][sum][mod];
        int ans=0,s;
        s=lim?a[pos]:9;
        for(int i=0;i<=s;i++)
        {
            int temp=(mod*10+i)%k;
            ans+=dfs(pos-1,lim&&i==s,k,sum+i,temp);
        }
        if(!lim) dp[pos][k][sum][mod]=ans;
        return ans;
    }
    int solve(long long x)
    {
        int cnt=0;
        while(x!=0)
        {
            ++cnt;
            a[cnt]=x%10;
            x/=10;
        }
        int ans=0;
        for(int i=1;i<=(cnt)*9;i++)
        {
            ans+=dfs(cnt,1,i,0,0);
        }
        return ans;
    }
    int main()
    {
        memset(dp,-1,sizeof(dp));
        int t;
        scanf("%d",&t);
        int cas=0;
        while(t--)
        {
            cas++;
            long long temp1,temp2;
            scanf("%lld%lld",&temp1,&temp2);
            //cout<<solve(temp1)<<" "<<solve(temp2)<<endl;
            printf("Case %d: %d
    ",cas,solve(temp2)-solve(temp1-1));
        }
    }
  • 相关阅读:
    解决centos yum源配置出现Couldn't resolve host 问题
    Centos7下MongoDB下载安装详细步骤
    PHP操作mongodb扩展的坑 及php7安装mongodb扩展
    阿里云 Composer 全量镜像
    centos beanstalkd 安装 与php调用
    centos与windows共享文件夹
    centos php 安装编译 常见报错
    [PHP] layui实现多图上传,图片自由排序,自由删除
    Vue-element-admin实现菜单根据用户权限动态加载
    迭代器的使用方法
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852310.html
Copyright © 2011-2022 走看看