X mod f(x)

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

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

21 10

11 20

Sample Output

Case 1: 10

Case 2: 3

————————————————————————切割线—————————————————————

记忆化搜索:

数据范围是10的9次方。数位和最大为9*9=81,枚举数位和

每次要维护:pos维数,当前数位和a。枚举的数位和b。当前的模mo。有无上限e

dp [pos] [a] [b] [mo];

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<cctype>
#define inf 0x3f3f3f3f
#define maxn 10000
typedef long long LL;
using namespace std;
int dp[11][82][82][82];
int num[11],pos,n,m;
int dfs(int pos,int a,int b,int mo,int e)
{
if(pos==-1) return a==b&&!mo;
if(!e&&dp[pos][a][b][mo]!=-1) return dp[pos][a][b][mo];
int end=e?num[pos]:9;
int sum=0;
for(int i=0;i<=end;i++){
sum+=dfs(pos-1,a+i,b,(mo*10+i)%b,e&&i==end);
}
return e?sum:dp[pos][a][b][mo]=sum;
}
void Init(int x)
{
pos=0;
while(x){
num[pos++]=x%10;
x/=10;
}
}
int cal(int x)
{
Init(x);
int res=0;
for(int i=1;i<=81;i++){
res+=dfs(pos-1,0,i,0,1);
}
return res;
}
int main()
{
int t,iCase=1;
memset(dp,-1,sizeof dp);
cin>>t;
while(t--){
scanf("%d%d",&m,&n);
printf("Case %d: %d\n",iCase++,cal(n)-cal(m-1));
} return 0;
}

HDU 4389——X mod f(x)(数位DP)的更多相关文章

  1. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  2. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  3. 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, ...

  4. 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 ...

  5. HDU 4734 F(x) ★(数位DP)

    题意 一个整数 (AnAn-1An-2 ... A2A1), 定义 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1,求[0..B]内有多少 ...

  6. HDOJ 4389 X mod f(x)

    数位DP........ X mod f(x) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. 【HDU 3709】 Balanced Number (数位DP)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  8. HDU 5642 King's Order【数位dp】

    题目链接: http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=677&pid=1003 题意: 求长度为n的序列 ...

  9. HDU - 4352 - XHXJ's LIS(数位DP)

    链接: https://vjudge.net/problem/HDU-4352 题意: a 到 b中一个数组成递增子序列长度等于k的数的个数 思路: 因为只有10个数,使用二进制维护一个递增序列,每次 ...

随机推荐

  1. isEqual

    "; NSString *str2 = [NSString stringWithFormat:@"%@", str1]; 大家明白, str1和str2在内存中的地址是不 ...

  2. 根据WSDL生成代理类方式(2)

    运行开发人员工具提示 输入命令行svcutil http://localhost:8080/Test/TestClassPort?wsdl

  3. redis基本命令的演示:

    import redis r = redis.Redis(host='127.0.0.1', port=6379,db = 0) #查看匹配redis的数据 r.keys() #查看redis的大小 ...

  4. Essential Sublime Text Plugins

    Essential Sublime Text Plugins Add some killer tools to your arsenal. View them all at /repo/sublime ...

  5. OSI/RM网络7层体系

    转自OSI/RM网络7层体系 1 物理层 这是整个OSI参考模型的最低层,它的任务就是提供网络的物理连接.所以,物理层是建立在物理介质上(而不是逻辑上的协议和会话),它提供的是机械和电气接口.主要包括 ...

  6. 在TMemo上画一条线

    var C:TControlCanvas; begin C := TControlCanvas.Create; C.Pen.Color := clRed; C.Pen.Width := ; C.Con ...

  7. Application对象的使用-数据传递以及内存泄漏

    Application的使用 What is Application Application和Activity,Service一样是android框架的一个系统组件,当android程序启动时系统会创 ...

  8. BroadCastReceiver中耗时操作导致ANR

    現象:廣播接收器中進行耗時的I/O操作導致ANR. 查資料發現每次广播到来时 , 会重新创建 BroadcastReceiver 对象 , 并且调用 onReceive() 方法 , 执行完以后 该对 ...

  9. 使用SparseArray代替HashMap

    HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果.最近在做一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performanc ...

  10. 【Jade】

    Jade 模板引擎使用 Jade - 模板引擎 Jade - Template Engine