hdu 4389 X mod f(x) 数位dp

题链:http://acm.hdu.edu.cn/showproblem.php?pid=4389

X mod f(x)

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


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
 


数位dp学习链接:http://blog.csdn.net/cyendra/article/details/38087573


题意:相当于问区间内有多少数满足 X%(∑xi)==0。∑xi 是数字X的数位和。

做法:因为最多9位数,所以可以枚举∑xi,最大为81。 然后就是数位dp了。

sum是数位和,nwmod是取模结果,mod 是枚举的模

当数位和sum==mod而且,nwmod最后==0,成立计数。



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map> 

typedef long long LL;
const int maxn=81;
int dig[maxn];
int f[10][maxn][maxn][maxn];
//nwmod  数取模后 sum 数位和
LL dfs(int pos,int nwmod,int sum,int mod,int limit)
{
	if (pos<0) return sum==mod&&nwmod==0;
	if (!limit&&f[pos][nwmod][sum][mod]!=-1) 
		return f[pos][nwmod][sum][mod];
	LL res=0;
	int last=limit?dig[pos]:9;
	for (int i=0;i<=last;i++)
	{
		res+=dfs(pos-1,(nwmod*10+i)%mod,sum+i,mod,limit&&(i==last));
	}
	if (!limit) f[pos][nwmod][sum][mod]=res;
	return res;
}

LL solve(LL n){
	int len=0; 
	while (n)
	{
		dig[len++]=n%10;
		n/=10;
	}
	LL ans=0;
	for(int i=1;i<=81;i++)//枚举最后的mod
	{
		ans+=dfs(len-1,0,0,i,1);
	}
	return ans;
}
int main()
{
	int n;
	int t;
	int cas=1;
	scanf("%d",&t);
	int a,b;
	memset(f,-1,sizeof f);
	while(t--)
	{
		scanf("%d%d",&a,&b);
		if(a>b)
			swap(a,b); 
		printf("Case %d: %I64d\n",cas++,solve(b)-solve(a-1));
	}
	return 0; 
}

/*
2
1 10
11 20 

Sample Output
Case 1: 10
Case 2: 3 
*/

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值