当前位置:网站首页>HDU 3709 Balanced Number
HDU 3709 Balanced Number
2022-06-26 12:39:00 【YJEthan】
to calculate the number of balanced numbers in a given range [x, y].
2 0 9 7604 24324
Sample Output
10
一个数,选一个数位为支点,如果两边的数分别用数值乘以离支点的距离的和相等则为平衡数,如abcd若a*1=c*1+d*2则为平衡树,支点可以任一选,若0=b*1+c*2+d*3,也是平衡数
可以先算出y以内的平衡数然后减去x-1内的平衡数
首先枚举支点,然后枚举数位
数位dp
#include<bits\stdc++.h>
using namespace std;
typedef long long ll;
ll dp[20][20][2005];
ll a[20];
ll dfs(ll pos,ll o,ll s,ll lim)//当前数位,当前的支点,当前位置左边比右边重s,当前位是否达到了最大值
{
if(pos==-1)
return s==0;
if(s<0) return 0;//如果此时的左边的重量小于右边,则直接跳过,因为是从左往右计算
if(!lim&&dp[pos][o][s]!=-1)
return dp[pos][o][s];
ll ans=0;
int up=lim?a[pos]:9;
for(ll i=0;i<=up;i++)
{
ans+=dfs(pos-1,o,s+(pos-o)*i,lim&&i==up);
}
if(!lim) dp[pos][o][s]=ans;
return ans;
}
ll solve(ll n)
{
int i=0;
while(n)
{
a[i++]=n%10;
n/=10;
}
ll ans=0;
for(int j=0;j<i;j++)//枚举每一个支点
ans+=dfs(i-1,j,0,1);
return ans-(i-1);
}
int main()
{
int t;
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
while(t--)
{
ll n,m;
scanf("%I64d%I64d",&n,&m);
printf("%I64d\n",solve(m)-solve(n-1));
}
}边栏推荐
猜你喜欢
![[geek challenge 2019] rce me 1](/img/66/e135f7e5a7cbdeb5b697f3939a3402.png)
[geek challenge 2019] rce me 1

四类线性相位 FIR滤波器设计 —— MATLAB源码全集

Stream learning record

轻流完成与「DaoCloud Enterprise 云原生应用云平台」兼容性认证

Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan

Unit practice experiment 8 - using cmstudio to design microprogram instructions based on basic model machine (1)
![P5733 [deep foundation 6. example 1] automatic correction](/img/34/081dbd805593a92a86c3081d6772e3.png)
P5733 [deep foundation 6. example 1] automatic correction

软件测试 - 基础篇

Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology

倍福NC轴状态转移图解析
随机推荐
processing 函数translate(mouseX, mouseY)学习
手把手带你学会Odoo OWL组件开发(7):OWL项目实战使用
National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
记一次phpcms9.6.3漏洞利用getshell到内网域控
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
Guacamole installation
国标GB28181协议EasyGBS视频平台TCP主动模式拉流异常情况修复
File remote synchronization and backup artifact Rsync
[esp32-C3][RT-THREAD] 基于ESP32C3运行RT-THREAD bsp最小系统
5+API,清除应用缓存
Processing 多面体变化
源码学习:AtomicInteger类代码内部逻辑
710. random numbers in the blacklist
Angle de calcul POSTGIS
倍福将EtherCAT模块分到多个同步单元运行--Sync Units的使用
[esp32-c3][rt-thread] run RT-Thread BSP minimum system based on esp32c3
QT .pri 的建立与使用
Redis learning - 02 common data types, operation commands and expiration time
软件测试报告应该包含的内容?面试必问
Learning Processing Zoog