数位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. nginx实现http反向代理+负载均衡

    原理 反向代理:反向代理(reverse proxy)方式是指以代理来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  2. Windows下安装paramiko

    windows下python IDE我用的是pycharm 在pycharm下安装paramiko import paramiko后提示没有pycrypto这个模块 在pycharm下又安装不上pyc ...

  3. ARPSpoofing教程(三) - 捕获数据包

    1: #include"pcap.h" 2: //每次捕获到数据包时,libpcap都会自动调用这个回调函数 3: void packet_handler(u_char *para ...

  4. BZOJ1933: [Shoi2007]Bookcase 书柜的尺寸

    传送门 很容易看出来这是一道DP题,那么怎么设置状态就成了这道题的关键.本题有点特殊的地方是有两个维度的状态,而每个维度又有三个部分的参数,如果全部设置出来的话肯定会MLE.首先对书的厚度状态简化. ...

  5. liunx 的 grep命令(转载)

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  6. angularjs中ng-selected使用方法

    ng-selected只能应用在option标签上,就像ng-submit只能应用在form标签上一样. ng-selected指令为select设置了指定的选中值,HTML规范不允许浏览器保存类似s ...

  7. Java——URL和URLConnection

    使用URL读取内容 import java.awt.im.InputContext; import java.io.InputStream; import java.net.MalformedURLE ...

  8. php函数ob_start()、ob_end_clean()、ob_get_contents()

    下面3个函数的用法 ob_get_contents() - 返回输出缓冲区的内容 ob_flush() - 冲刷出(送出)输出缓冲区中的内容 ob_clean() - 清空(擦掉)输出缓冲区 ob_e ...

  9. 一种nodejs的MVC框架

    mvc会针对请求进行分发,分发一般有controller(针对模块),action(针对模块中的方法),args(请求的参数). 1.先对http请求的url进行设置,解析url中的各种参数: //c ...

  10. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast

    Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast的解决方法: 在命令行输入:yum clean al ...