HDU 4389 数位dp

X mod f(x)

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

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

2012 Multi-University Training Contest 9

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define dazhi 2147483647
12 #define bug() printf("!!!!!!!")
13 #define M 100005
14 using namespace  std;
15 int bit[10];
16 int dp[10][82][82][82];
17 int  n;
18 int  functi(int pos,int mod,int xx ,int sum,bool flag)
19 {
20     if(pos==0) return (xx==sum&&mod%sum==0);
21     if(flag&&dp[pos][mod][xx][sum]!=-1) return dp[pos][mod][xx][sum];
22     ll x=flag ? 9 : bit[pos];
23     ll ans=0;
24     for(ll i=0;i<=x;i++)
25     ans+=functi(pos-1,(mod*10+i)%xx,xx,sum+i,flag||i<x);
26     if(flag)
27         dp[pos][mod][xx][sum]=ans;
28     return ans;
29 }
30 int  fun(int x)
31 {
32     int len=0;
33     while(x)
34     {
35         bit[++len]=x%10;
36         x/=10;
37     }
38     ll re=0;
39     for(int i=1;i<=81;i++)
40         re+=functi(len,0,i,0,0);
41     return re;
42 }
43 int main()
44 {
45    int t;
46    int  l,r;
47    while(scanf("%d",&t)!=EOF)
48    {
49        memset(dp,-1,sizeof(dp));
50        for(int i=1;i<=t;i++)
51        {
52            scanf("%d %d",&l,&r);
53            int exm=fun(r);
54            printf("Case %d: %d\n",i,exm-fun(l-1));
55        }
56    }
57     return 0;
58 }
时间: 2024-07-28 13:59:16

HDU 4389 数位dp的相关文章

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了.dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数. #include <bits/stdc++.h> using namespace std; typedef long

hdu 5179 数位dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1

HDU 4352 数位dp

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3081    Accepted Submission(s): 1291 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefu

HDU 3709 数位dp

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5276    Accepted Submission(s): 2523 Problem Description A balanced number is a non-negative integer that can be balanced if a pi