当前位置:网站首页>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));
}
}边栏推荐
猜你喜欢

Processing polyhedron change

IDC报告:百度智能云AI Cloud市场份额连续六次第一
软件测试报告应该包含的内容?面试必问

Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏

Learning Processing Zoog
What should the software test report include? Interview must ask

Solution of Splunk iowait alarm

Electron official docs series: Processes in Electron

利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)

Mode pont
随机推荐
C# 结构体:定义、示例
OPLG: 新一代云原生可观测最佳实践
复制多个excel然后命名不同的名字
E - Apple Catching
H - Sumsets POJ 2229
Copy multiple Excel files and name them different
倍福TwinCAT通过Emergency Scan快速检测物理连接和EtherCAT网络
Script - crawl the customized storage path of the cartoon and download it to the local
National standard gb28181 protocol easygbs cascaded universal vision platform, how to deal with live message 403?
Dark horse notes - Common APIs
心脏滴血漏洞(CVE-2014-0160)分析与防护
Processing function translate (mousex, mousey) learning
IDC report: the AI cloud market share of Baidu AI Cloud ranks first for six consecutive times
橋接模式(Bridge)
UVa11582 [快速幂]Colossal Fibonacci Numbers!
C structure: definition and example
Angle de calcul POSTGIS
G - Cow Bowling
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
Electron official docs series: Testing And Debugging