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

问有多少个数字是“平衡”的,平衡就是取个参考点,左边的数字*距离=右边的数字*距离,4139以3为参考点就是4*2+1*1==9*1

一个数字的参考点唯一。

枚举参考点是第几位,然后对于每一个参考点的位数k,分别跑数位dp,记一下参考点位置,左边的力矩,有没有前导零。在参考点右边的时候直接在力矩上面减即可。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define int long long
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL n,len,l,r;
LL f[][][][];
int d[];
int zhan[],top;
inline LL dfs(int now,int p,int M,int lead,int fp)
{
if (now==)return !M;
if (!fp&&f[now][p][M][lead]!=-)return f[now][p][M][lead];
LL ans=,mx=fp?d[now-]:;
for (int i=;i<=mx;i++)
{
int delta=i*(now--p);
if (M+delta<)break;
if (now-==p&&i==&&lead&&now-!=)continue;
ans+=dfs(now-,p,M+delta,lead&&i==&&now-!=,fp&&i==mx);
}
if (!fp)f[now][p][M][lead]=ans;
return ans;
}
inline LL calc(LL x)
{
if (x==-)return ;
if (x==)return ;
LL xxx=x;
len=;
while (xxx)
{
d[++len]=xxx%;
xxx/=;
}
LL sum=;
for (int i=;i<=len;i++)
{
for (int j=;j<=d[len];j++)
if (!(j==&&i==len)||len==)
{
sum+=dfs(len,i,j*(len-i),j==&&len!=,j==d[len]);
}
}
return sum;
}
main()
{
int T=read(),cnt=;
memset(f,-,sizeof(f));
while (T--)
{
l=read();
r=read();
if (r<l)swap(l,r);
printf("%lld\n",calc(r)-calc(l-));
}
}

hdu 3709

最新文章

  1. java反射 之 反射基础
  2. python基础 Day01 练习题
  3. [译]git log
  4. microsofr visual studio编写c语言
  5. MongoDB学习笔记——索引管理
  6. nginx: [error] invalid PID number &quot;&quot; in &quot;/usr/local/nginx/logs/nginx.pid&quot;
  7. Vue.js常见问题
  8. 常用WEB服务器的特点介绍
  9. HTML5 文件处理之FileAPI简介整理
  10. centos 6.5 安装阿里云的一键安装包(nginx+php5.4+mysql5.1)
  11. ObjectARX自定义实体的最近点和垂点捕捉算法
  12. font-face 在 Firefox无法正常工作问题
  13. 缺少新的栈标识:报出异常FLAG_ACTIVITY_NEW_TASK flag-是由于activity关闭之后开启一个新的acitivity时没有标识在栈中,所以需要给一个栈标识
  14. 问题:页面输出正常,php写入sqlserver乱码/空白。
  15. 坑 flutter Positioned相关
  16. 《JavaScript Dom 编程艺术》读书笔记-第4章
  17. [BZOJ3674]可持久化并查集加强版&amp;[BZOJ3673]可持久化并查集 by zky
  18. nginx面试中最常见的18道题
  19. SQL SERVER_Restore(version)
  20. 【Spark】SparkStreaming-Checkpoint-容错

热门文章

  1. HTML之元素分类
  2. javaweb基础(1)_入门
  3. java基础—配置环境变量
  4. bootstrap历练实例:面板脚注
  5. dht 分布式hash 一致性hash区别
  6. C# 使用Epplus导出Excel [5]:样式
  7. 洛谷 P1147 连续自然数和
  8. 转 Hystrix入门指南 Introduction
  9. 初识Mysql之基本简单语法总结
  10. python入门:if和else的基本用法