数位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. shell命令date

    某个标准时间转换为unix时间戳 date -d '2015-10-20 15:07:02' +%s unix时间戳转换为对应的标准时间 date -d @1445324822 date " ...

  2. iptables log

    iptables 日志 LOG target 这个功能是通过内核的日志工具完成的(rsyslogd) LOG现有5个选项 --log-level debug,info,notice,warning,w ...

  3. K米APP案例分析

    关于 K米 -- 的案例分析 产品 K米的APP (全国KTV点歌,手机直播,互动,交友,预订)的Android客户端 第一部分 调研,评测 评测: 软件的bug,功能评测,黑箱测试 • 下载并使用, ...

  4. 【Beta】Daily Scrum 第一天

    [目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 已完成 接下来要完成的 612 添加计时界面返回按键事件,添加SharePreferenc ...

  5. Linux以外的开源操作系统大汇总

    开源操作系统即公开源代码的操作系统软件,它遵循开源协议使用.编译和发布.自由和开放源代码软件中最著名的是Linux,它是一种类Unix的操作系统.Linux可安装在各种计算机硬件设备中,比如手机.平板 ...

  6. 在linux下安装tesseract-ocr

    1. 在ubuntu下可以自动安装  [html]   技术分享技术分享    sudo apt-get install tesseract-ocr  2.编译安装    a.编译环境: gcc gc ...

  7. 9.12 其他样式;JS

    Display 显示block和隐藏none,不占位置Visbility 显示visible和隐藏hidden,占位置Overflow 超出范围 hidden隐藏透明圆角 Js脚本语言(JavaScr ...

  8. matlab注释

    单行注释: 两个'%': 多行注释: A. %{ 若干语句 %} B. 注释: 选中要注释的若干语句, 编辑器菜单Text->Comment, 或者快捷键Ctrl+R 取消注释: 选中要取消注释 ...

  9. c#.net WinForm 线程内 调用窗体控件

    richTextBox1.BeginInvoke(new EventHandler(delegate { richTextBox1.AppendText("正在提交服务器..\r\n&quo ...

  10. Memcached深度分析

    Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能.关于这个东西,相信很多人都用过,本文意在通过对 ...