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. 【原创】Mvc学习笔记(1)

    1.新建MVC4项目 在MVC4中有App_Data文件夹,这个文件夹里可以放一些重要的数据,比如说数据库的mdf文件等等,这个文件夹非常安全,因为这个文件夹不允许被别人下载,不允许被浏览器访问. A ...

  2. C++语言十进制数,CDecimal(未完成)

    在C#和Java中都有存在decimal类似的十进制数字,C++中尚未发现,春节假期忙里抽闲写了一个玩玩,时间紧迫没有测试,只能保证编译通过.抛砖引玉,欢迎大家多提建议 当前缺陷: 1. 除法功能没有 ...

  3. SVN - 基础知识

    1. 术语 $ svn checkout  URL [PATH] -----   下载服务器所有文件 (clone) 到本地[path]  --- 只需一次 $ svn checkout  http: ...

  4. 简谈ArrayList和LinkedList区别

    对于ArrayList和LinkedList,他们都实现了List接口,他们的区别大致为: ArrayList LinkedList (1)底层是数组,可以以O(1)的时间复杂度对元素进行随机访问 以 ...

  5. Stanford CoreNLP--Part of Speech

    Stanford CoreNLP Part Of Speech简称POS,主要是对待分析的句子中的单词进行标记的功能,如标记名词.动词等,该组件是CoreNLP工程的一部分,详细内容可参考:CoreN ...

  6. Jquery UI dialog 传参

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3519318.html] $("#dialog").dialog({ aut ...

  7. 运行 maven install的时候出现错误 not a jre

    原文转自jingyan.baidu.com/article/c85b7a6464d8be003bac95fb.html (linux下我直接执行第二步,错误解决) 在使用eclipse 运行 mave ...

  8. [转贴]WebService的简单实现 C++

    WebService的简单实现 一.socket主机创建和使用过程 1.socket()//创建套接字 2.Setsockopt()//将套接字属性设置为允许和特定地点绑定 3.Bind()//将套接 ...

  9. [转贴]Windows下gSoap交叉编译环境的搭建

    本人直接就用过gSoap,它是用以C/C++写webservice的利器     交叉编译的时候,有两个很关键的程序:         soapcpp2.exe         wsdl2h.exe ...

  10. 備份Sqlite DB到XML文件:

    转载请注明出处:http://blog.csdn.net/krislight 项目中遇到备份与还原App数据的需求,需要把DB数据备份到一个XML文件中,然后保存到SD卡上,还原的时候直接从XML文件 ...