数位DP........

X mod f(x)

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

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
 
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int bit[10],dp[10][82][82][82];

int dfs(int pos,int sum,int mod,int res,bool limit)
{
    if(mod>81||sum>mod) return 0;
    if(pos==-1) return sum==mod&&res==0;
    if(!limit&&~dp[pos][sum][mod][res])
        return dp[pos][sum][mod][res];
    int end=limit?bit[pos]:9,ans=0;
    for(int i=0;i<=end;i++)
    {
        ans+=dfs(pos-1,sum+i,mod,(res*10+i)%mod,limit&&i==end);
    }
    if(!limit)
        dp[pos][sum][mod][res]=ans;
    return ans;
}

int calu(int x)
{
    int pos=0,ans=0;;
    while(x)
    {
        bit[pos++]=x%10;
        x/=10;
    }
    for(int i=1;i<=pos*9;i++)
    {
        ans+=dfs(pos-1,0,i,0,true);
    }
    return ans;
}

int main()
{
    int t,a,b,cas=1;
    memset(dp,-1,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&a,&b);
        printf("Case %d: %d\n",cas++,calu(b)-calu(a-1));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 4389 X mod f(x)的更多相关文章

  1. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  2. HDU 4389——X mod f(x)(数位DP)

    X mod f(x) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  3. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  4. HDU 4389 X mod f(x)

    题意:求[A,B]内有多少个数,满足x % f(x) == 0. 解法:数位DP.转化为ans = solve(b) - solve(a - 1).设dp[i][sum][mod][r]表示长度为i, ...

  5. HDU4389:X mod f(x)(数位DP)

    Problem Description Here is a function f(x): int f ( int x ) { if ( x == 0 ) return 0; return f ( x ...

  6. HDU X mod f(x)(题解注释)

    X mod f(x) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. hdu4389 X mod f(x)

    链接 这个题因为总和加起来是比较小的9*9 = 81  这样可以保留前面枚举的数对所有的可能出现的和的余数,然后依次向下找. #include <iostream> #include< ...

  8. 动态规划 is beginning。。。。。。。。。

    感觉动态规划非常模糊,怎么办呢??? 狂刷题吧!! !! ! !!! ! !!! !! ! ! ! .!! ..!.! PKU  PPt 动规解题的一般思路 1. 将原问题分解为子问题         ...

  9. 基础数位DP小结

    HDU 3555 Bomb dp[i][0] 表示含 i 位数的方案总和. sp[i][0] 表示对于位数为len 的 num 在区间[ 10^(i-1) , num/(10^(len-i)) ] 内 ...

随机推荐

  1. COGS 577 蝗灾

    传送门 时间限制:2 s 内存限制:128 MB DESCRIPTION C国国土辽阔,地大物博......但是最近却在闹蝗灾..... 我们可以把C国国土当成一个W×W的矩阵,你会收到一些诸如(X, ...

  2. HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)

    题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS     Memory Limit: 32768 K Description The inve ...

  3. Beta版本——第四次冲刺博客

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  4. 什么是jsonp

    Jsonp其实就是一个跨域解决方案. Js跨域请求数据是不可以的,但是js跨域请求js脚本是可以的. 所以可以把要请求的数据封装成一个js语句,做一个方法的调用. 跨域请求js脚本可以得到此脚本.得到 ...

  5. Yocto开发笔记之《驱动调试-华为3G模块》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 参考:ht ...

  6. spring-data-jpa Repository的基本知识

    1.项目中的Repository对象的使用 2.Repository 引入的两种方式 继承和使用注解 3.Repository接口的定义 Repository 接口是 spring Data 的一个核 ...

  7. SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法

    今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...

  8. MYSQL版本问题:解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future.

  9. python的简洁是shell无法代替的

    之前线上服务器分发配置都是用shell和expect脚本分发,脚本写了很长,上周换了ansible,现在自己用python写一个,就30行代码就可以实现需求,之前的shell写了快200行了,蛋疼,代 ...

  10. Linux--文件查找命令

    一.简介查找文件的几个命令: 1.which :查找可执行文件的位置 2.whereis:查找文件的位置,可以找到可执行命令和man page 3.locate:配合数据库查看文件位置 4.find: ...