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 <= 10
9), 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
 

题意:计算区间内一个数字各位之和能整除该数字的个数

思路:

分别计算出[1, b]中符合条件的个数和[1, a-1]中符合条件的个数。

d[l][i][j][k]表示前l位和为i模j的结果为k的数的个数,那么就有方程

d[l+1][i+x][j][(k*10+x)%j] += d[l][i][j][k]

预处理出d[l][i][j][k],然后再逐位统计即可

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int bit[10];
int dp[10][82][82][82];
//d[l][i][j][k]表示前l位和为i模j的结果为k的数的个数
void set()
{
int i,j,k,l,x;
for(i = 1; i<=81; i++)
dp[0][0][i][0] = 1;
for(l = 0; l<9; l++)
for(i = 0; i<=l*9; i++)
for(j = 1; j<=81; j++)
for(k = 0; k<j; k++)
for(x = 0; x<=9; x++)
dp[l+1][i+x][j][(k*10+x)%j] += dp[l][i][j][k];
} int solve(int n)
{
if(!n)
return 0;
int ans,i,j,k,len;
int sum,tem1,tem2,s,bit[10],r;
len = sum = ans = 0;
tem1 = tem2 = n;
s = 1;
while(tem1)
{
bit[++len] = tem1%10;
tem1/=10;
sum+=bit[len];//每位数之和
}
if(n%sum==0)//本身要先看是否整除
ans++;
for(i = 1; i<=len; i++)
{
sum-=bit[i];//将该位清0
tem2/=10;
s*=10;
tem1 = tem2*s;
for(j = 0; j<bit[i]; j++) //枚举该位的状况
{
for(k = sum+j; k<=sum+j+9*(i-1); k++) //该位与更高位的和,而比该位低的和择优9*(i-1)种
{
if(!k)//和为0的状况不符合
continue;
r = tem1%k;//现在该数对各位和进行取余
if(r)
r = k-r;//余数大于0,那么k-dd得到的数肯定能被t整除
ans+=dp[i-1][k-sum-j][k][r];//加上个数
}
tem1+=s/10;//标记现在算到哪里,例如1234,一开始t是1230,然后1231,1232,1233,1234,接下来1200,就是1210,1220,1230
}
}
return ans;
} int main()
{
int T,l,r,cas = 1;
set();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&l,&r);
printf("Case %d: %d\n",cas++,solve(r)-solve(l-1));
} return 0;
}

HDU4389:X mod f(x)(数位DP)的更多相关文章

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

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

  2. hdu4734 F(x)(数位dp)

    题目传送门 F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

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

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

  4. 【hdu4734】F(x) 数位dp

    题目描述 对于一个非负整数 $x=​​\overline{a_na_{n-1}...a_2a_1}$ ,设 $F(x)=a_n·2^{n-1}+a_{n-1}·2^{n-2}+...+a_2·2^1+ ...

  5. [hdu4734]F(x)数位dp

    题意:求0~f(b)中,有几个小于等于 f(a)的. 解题关键:数位dp #include<bits/stdc++.h> using namespace std; typedef long ...

  6. F(x) 数位dp

    Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight ...

  7. HDU-4734 F(x) 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 注意到F(x)的值比较小,所以可以先预处理所有F(x)的组合个数.f[i][j]表示 i 位数时 ...

  8. HDU 4734 - F(x) - [数位DP][memset优化]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 Time Limit: 1000/500 MS (Java/Others) Memory Lim ...

  9. hdu4389 X mod f(x)

    链接 这个题因为总和加起来是比较小的9*9 = 81  这样可以保留前面枚举的数对所有的可能出现的和的余数,然后依次向下找. #include <iostream> #include< ...

随机推荐

  1. LINQ.CS

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Zdso ...

  2. windows32下安装zend framework2

    首先安装好php(5.3.3以上).apache和mysql apache 开启mod_rewrite 模块 将所有AllowOverride None设置为AllowOverride FileInf ...

  3. HighCharts学习笔记

    目录 xAxis自定义时间刻度的显示 xAxis自定义时间刻度 我们先来看下HighCharts图表的xAxis对象有哪些属性(红色标记重要属性): allowDecimals: Booleancat ...

  4. Android--消除“Permission is only granted to system apps”错误

    原文:http://blog.csdn.net/gaojinshan/article/details/14230673 在AndroidManifest.xml中使用了如下的配置: <uses- ...

  5. AVPlayer的基本使用

    2014-5-7 06:46| 发布者: admin| 查看: 437| 评论: 0   摘要: 在iOS开发中,播放视频通常有两种方式,一种是使用MPMoviePlayerController(需要 ...

  6. phpcms v9 企业黄页系统发布没有表单出现的解决方案

    第一种解决方案: 第一步:把yp_UTF8压缩文件解压得到:api.caches.phpcms.statics四个文件夹. 第二步:把这四个文件夹分别覆盖已安装好的phpcms系统根目录下的文件夹.这 ...

  7. 谈谈php中上传文件的处理

    这是一个表单的时代... 我们在浏览器中编辑自己的信息,会遇到上传头像:在文库中,我们会上传文档......到处存在“上传”这个词. php是最好的语言(其他语言的程序猿们不要打我...).php在处 ...

  8. 从状态转移看:载波侦听多路访问/冲突避免(CSMA/CA)

    CSMA/CA是写入IEEE802.11的无线网络MAC层标准协议,相信看到这篇文章的读者都知道它是用来做什么的.但许多短文对这个协议的解释都有所缺乏,因此本文用状态转换图的形式详细说明协议的工作流程 ...

  9. 1257: [CQOI2007]余数之和sum - BZOJ

    Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, ...

  10. ApplicationContext

    参考网址: http://baike.baidu.com/link?url=IPzNiVScxSd6ijhDeCKKEuywPqisDeTfyYSQIPRZqLxy6onkPddfzyvcWQC6_M ...