当前位置:网站首页>HDU 3709 Balanced Number
HDU 3709 Balanced Number
2022-06-26 13:12:00 【YJEthan】
to calculate the number of balanced numbers in a given range [x, y].
2 0 9 7604 24324
Sample Output
10
a number , Choose a digit as the fulcrum , If the sum of the numbers on both sides multiplied by the distance from the fulcrum is equal, it is the equilibrium number , Such as abcd if a*1=c*1+d*2 Is a balanced tree , You can choose any fulcrum , if 0=b*1+c*2+d*3, It is also an equilibrium number
You can work it out first y And then subtract x-1 Equilibrium number in
First enumerate the fulcrum , Then enumerate the digits
digit 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)// The current number of digits , Current fulcrum , The left side of the current position is heavier than the right side s, Whether the current bit has reached the maximum value
{
if(pos==-1)
return s==0;
if(s<0) return 0;// If the weight of the left side is less than that of the right side , Then skip directly , Because it is calculated from left to right
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++)// Enumerate every fulcrum
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));
}
}
边栏推荐
猜你喜欢
IDC report: the AI cloud market share of Baidu AI Cloud ranks first for six consecutive times
详细讲解C语言11(C语言系列)
Electron official docs series: Processes in Electron
倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络
Summary of wechat applet test points
opencv高速下载
Mode pont
National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology
机器学习笔记 - 时间序列的季节性
随机推荐
倍福PLC选型--如何看电机是多圈绝对值还是单圈绝对值编码器
B - Bridging signals
Guacamole installation
倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
postgis 地理化函数
偶言佳句,孤芳自赏
To solve the difficulties of small and medium-sized enterprises, Baidu AI Cloud makes an example
Record a phpcms9.6.3 vulnerability to use the getshell to the intranet domain control
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
Go 结构体方法
Explain C language 11 in detail (C language series)
Don't mess with full_ Case and parallel_ CASE
Stream learning record
Uva10341 solve it
适配器模式(Adapter)
Electron official docs series: References
8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
postgis计算角度
Typescript
QT .pri 的建立与使用