数位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. C#皮肤制作

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  2. python——复制目录结构小脚本

    引言 有个需要,需要把某个目录下的目录结构进行复制,不要文件,当目录结构很少的时候可以手工去建立,当目录结构复杂,目录层次很深,目录很多的时候,这个时候要是还是手动去建立的话,实在不是一种好的方法,弄 ...

  3. androidstudio 常用快捷键

    ctrl+p  查看需要输入的函数列表 ctrl+h  查看继承关系 ctrl +w   选择一个,一行,一段. alter+enter  内容助理 ctrl+q   查看方法注解 相当于 hover ...

  4. AngularJs angular.uppercase、angular.lowercase、angular.fromJson、angular.toJson

    angular.uppercase 将指定的字符串转换成大写 格式:angular.uppercase(string); string:被转换成大写的字符串. 使用代码: var str = &quo ...

  5. Java 线程池的使用

    转载原文链接: http://www.cnblogs.com/dolphin0520/p/3932921.html 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有 ...

  6. CentOS terminal 安装 matlab(mode=silent)

    1. 下载matlab for Unix 2014 ,需要crack文件 2. 挂载iso文件, mount -o loop,ro Mathworks.Matlab.R2014a.iso /media ...

  7. JPA事务总结

    http://www.soso.io/article/65405.html 事务管理是JPA中另一项重要的内容,了解了JPA中的事务管理,能够进一步掌握JPA的使用.事务管理是对一系列操作的管理,它最 ...

  8. 通过命令行连接Wifi

    前提:无线网卡驱动正常安装 1.检查连接无线的接口 $ iwconfig 一般无线接口为wlan0 2.检查无线接口是否工作 $ sudo ip link set wlan0 up 3.扫描周围无线网 ...

  9. ASP.NET 递归将分类绑定到 TreeView

    CREATE TABLE [dbo].[sysMenuTree]([NoteId] [decimal](18, 0) NOT NULL,[ParentId] [decimal](18, 0) NULL ...

  10. ----------jqery和js如何判断checkbox是否选中 --------两个单选按钮如何选一个,且用jquery获取被选的值

    jqery和js如何判断checkbox是否选中 jquery: <div id="divId" class="divTable"> <div ...