hdu3709 Balanced Number (数位dp+bfs)

本文介绍了一种计算特定范围内平衡数数量的算法。平衡数是指一个非负整数,通过在其某个位上放置支点,使得左右两边的力矩相等。文章提供了两段AC代码实现,详细解释了如何通过递归深度优先搜索来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Balanced Number

Problem Description

A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. Then the torques of left part and right part can be calculated. It is balanced if they are the same. A balanced number must be balanced with the pivot at some of its digits. For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part, respectively. It's your job
to calculate the number of balanced numbers in a given range [x, y].

 

Input

The input contains multiple test cases. The first line is the total number of cases T (0 < T ≤ 30). For each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 1018).

 

Output

For each case, print the number of balanced numbers in the range [x, y] in a line.

 

Sample Input

2

0 9

7604 24324

 

Sample Output

10

897

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
int digit[20];
__int64 dp[20][20][10001];
__int64 dfs(int pos,int c,int l,int lim)
{
    if(pos==0) return l==0;
    if(l<0) return 0;
    if(!lim && dp[pos][c][l]!=-1) return dp[pos][c][l];
    int n=lim?digit[pos]:9;
    __int64 ans=0;
    for(int i=0; i<=n; i++)
    {
        int next=l;
        next+=(pos-c)*i;
        ans+=dfs(pos-1,c,next,lim&&i==n);
    }
    if(!lim) dp[pos][c][l]=ans;
    return ans;
}
__int64 solve(__int64 n)
{
    int len=0;
    while(n)
    {
        digit[++len]=n%10;
        n/=10;
    }
    __int64 sum=0;
    for(int i=1; i<=len; i++) sum+=dfs(len,i,0,1);
    return sum-len+1;
}
int main()
{
    int t;
    __int64 x,y;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));
    while(t--)
    {
        scanf("%I64d%I64d",&x,&y);
        printf("%I64d\n",solve(y)-solve(x-1));
    }
    return 0;
}


另一个带注释的AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int bit[19];
__int64 dp[19][19][2005];
//pos为当前位置
//o为支点
//l为力矩
//work为是否有上限
__int64 dfs(int pos,int o,int l,int work)
{
    if(pos==-1)  return l==0;//已经全部组合完了
    if(l<0)  return 0;//力矩和为负,则后面的必然小于0
    if(!work && dp[pos][o][l]!=-1) return dp[pos][o][l];//没有上限,且已经被搜索过了
    __int64 ans=0;
    int end=work?bit[pos]:9;//有上限就设为上限,否则就设为9
    for(int i=0; i<=end; i++)
    {
        int next=l;
        next+=(pos-o)*i;//力矩
        ans+=dfs(pos-1,o,next,work&&i==end);
    }
    if(!work) dp[pos][o][l]=ans;
    return ans;
}
__int64 solve(__int64 n)
{
    int len=0;
    while(n)
    {
        bit[len++]=n%10;
        n/=10;
    }
    __int64 ans = 0;
    for(int i=0; i<len; i++)  ans+=dfs(len-1,i,0,1);
    return ans-(len-1);//排除掉0,00,000....这些情况
}

int main()
{
    int T;
    __int64 l,r;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--)
    {
        scanf("%I64d%I64d",&l,&r);
        printf("%I64d\n",solve(r)-solve(l-1));
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/a-clown/p/5917879.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值