HDU - 3709(数位DP)

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3709

依据题目意思,建立DP三维状态,dp[pos][mid][sum]表示第pos位以mid为中点建立的力矩值为sum的个数,

直接枚举每一位为中心点的情况即可,直接上个板子。

(但是莽夫做题选手注意一下全为0的情况,需要减去)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
const ll mod=1000000007;
int a[70];
ll dp[25][25][2000];
ll dfs(int pos,int mid,int sum,bool limit)
{
	if(pos<1)
	{
		if(sum==0)
		return 1;
		return 0;
	}
    if(!limit&&dp[pos][mid][sum]!=-1)
	return dp[pos][mid][sum];
    int up=limit?a[pos]:9;
   	ll temp=0;
    for(int i=0;i<=up;i++)
    {
    	temp+=dfs(pos-1,mid,sum+(pos-mid)*i,limit&&i==up);
    }
    if(!limit)
	dp[pos][mid][sum]=temp;
    return temp;
}
ll solve(ll n)
{ 
	if(n<0)
	return 0; 
	int len=1;
	while(n)
	{
		a[len++]=n%10;
		n/=10;
	}
	ll ans=0;
	for(int i=1;i<len;i++)
	ans+=dfs(len-1,i,0,true);
	return ans-(len-1)+1;
}
int main()
{
	memset(dp,-1,sizeof(dp));
	int T;
	scanf("%d",&T);
	while(T--)
	{
		ll l,r;
		scanf("%I64d %I64d",&l,&r);
		printf("%I64d\n",solve(r)-solve(l-1));
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值