hdu 4722 Good Numbers(数位dp)

Good Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3453    Accepted Submission(s): 1090

Problem Description

If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.

You are required to count the number of good numbers in the range from A to B, inclusive.

Input

The first line has a number T (T <= 10000) , indicating the number of test cases.

Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 1018).

Output

For test case X, output "Case #X: " first, then output the number of good numbers in a single line.

Sample Input

2
1 10
1 20

Sample Output

Case #1: 0
Case #2: 1

Hint

The answer maybe very large, we recommend you to use long long instead of int.

Source

2013 ACM/ICPC Asia Regional Online —— Warmup2

Recommend

zhuyuanchen520

题意:求A到B之间   各个位数的和sum%10==0  的数的个数。

题解:数位dp,dp[i][j]表示当前i位mod10=j的个数。

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#define ll long long

using namespace std;

int n;

ll dp[20][20];
int num[20];
ll a,b;

ll dfs(int i,int mod,bool e) {
    if(i<=0)return mod?0:1;
    if(!e&&dp[i][mod]!=-1)return dp[i][mod];
    ll res=0;
    int u=e?num[i]:9;
    for(int d=0; d<=u; d++) {
        int Mod=(mod+d)%10;
        res+=dfs(i-1,Mod,e&&d==u);
    }
    return e?res:dp[i][mod]=res;
}

ll solve(ll x) {
    int len=1;
    ll k=x;
    while(k) {
        num[len++]=k%10;
        k/=10;
    }
    num[len]=0;
    return dfs(len-1,0,1);
}

int main() {
    //freopen("test.in","r",stdin);
    int t;
    memset(dp,-1,sizeof dp);
    cin>>t;
    int ca=1;
    while(t--) {
        scanf("%I64d%I64d",&a,&b);
        printf("Case #%d: %I64d\n",ca++,solve(b)-solve(a-1));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-05 07:54:16

hdu 4722 Good Numbers(数位dp)的相关文章

HDU 4722 Good Numbers (数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:数位dp,dp[i][j]表示到第i位,数字和%10为j,然后进行dp,注意完全匹配的情况是要+1,而其他情况是从0 到 9 都要考虑 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; int

hdu 4722 Good Numbers(初涉数位dp)

http://acm.hdu.edu.cn/showproblem.php?pid=4722 大致题意:若一个整数的各位数字之和是10的倍数,称这个数为"good number".给出区间[A,B],求出该区间内"good number"的数的个数. 第一道数位dp,折腾了半天才明白怎么回事. 设dp[site][mod]表示到第site位(由高位向低位)前面各位数字之和对10取余为mod的数的个数,进行记忆化搜索.有两个很重要的点,首先是变量up,表示是否到达边界

hdu 4722 Good Numbers(dp)

public static void main(String[] args) { String a=null; if("aa".equals(a))//这种情形,不出现空指针异常 //if(a.equals("aa"))//出现空指针异常 { System.out.println(true); } else { System.out.println(false); } } 上面的两句不同的比较语句测试,第一句不出现空指针异常,第二句出现. 所以在变量和常量比较的时候

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

[ACM] hdu 2089 不要62(数位Dp)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19043    Accepted Submission(s): 6442 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就

cf55dBeautiful numbers数位dp

想到 最小公倍数 其余的就好搞了 ,可是没想到 #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

Balanced Numbers数位dp

三进制搞下, 0  表示没出现过,  第i位为1 表示 i出现了奇数次,  2表示i 出现了偶数次. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #includ

HDU 2089 不要62(数位DP,三种姿势)

HDU 2089 不要62(数位DP,三种姿势) ACM 题目地址:HDU 2089 题意: 中文题意,不解释. 分析: 100w的数据,暴力打表能过 先初始化dp数组,表示前i位的三种情况,再进行推算 直接dfs,一遍搜一变记录,可能有不饥渴的全部算和饥渴的部分算情况,记录只能记录全部算(推荐看∑大的详细题解Orz) 代码: 1. 暴力 (以前写的) /* * Author: illuz <iilluzen[at]gmail.com> * File: 2089_bf.cpp * Create

HDU - 3555 Bomb (数位DP)

题意:求1-n里有多少人包含"49"的数字 思路:数位DP,分三种情况:到第i位没有49的情况,到第i位没有49且最高位是9的情况,到第i位有49的情况,将三种情况都考虑进去就是了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; long long dp[30][3], n; int arr