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

[暑假集训--数位dp]hdu3709 Balanced Number的更多相关文章

  1. [暑假集训--数位dp]hdu5787 K-wolf Number

    Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation o ...

  2. [暑假集训--数位dp]hdu5898 odd-even number

    For a number,if the length of continuous odd digits is even and the length of continuous even digits ...

  3. [暑假集训--数位dp]LightOj1205 Palindromic Numbers

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  4. [暑假集训--数位dp]hdu3555 Bomb

    The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...

  5. [暑假集训--数位dp]hdu3652 B-number

    A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...

  6. [暑假集训--数位dp]hdu2089 不要62

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍, ...

  7. [暑假集训--数位dp]LightOJ1140 How Many Zeroes?

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...

  8. [暑假集训--数位dp]LightOj1032 Fast Bit Calculations

    A bit is a binary digit, taking a logical value of either 1 or 0 (also referred to as "true&quo ...

  9. [暑假集训--数位dp]cf55D Beautiful numbers

    Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...

随机推荐

  1. 剑指offer46 求1+2+...+n 以及& &&区别

    参考代码: class Solution { public: int Sum_Solution(int n) { int result = n; result && (result + ...

  2. maven,gradle本地缓存位置

    gradle: 配置系统环境变量GRADLE_USER_HOME即可,值为缓存位置. maven: 修改settings文件:maven的home路径下的conf文件夹下的settings.xml 对 ...

  3. MySQL索引类型及优化

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...

  4. Nginx正向代理代理http和https服务

    Nginx正向代理代理http和https服务 1. 背景需求 通过Nginx正向代理,去访问外网.可实现局域网不能访问外网的能力,以及防止在上网行为上,留下访问痕迹. 2. 安装配置 2.1安装 w ...

  5. Python爬虫系列-分析Ajax请求并抓取今日头条街拍图片

    1.抓取索引页内容 利用requests请求目标站点,得到索引网页HTML代码,返回结果. 2.抓取详情页内容 解析返回结果,得到详情页的链接,并进一步抓取详情页的信息. 3.下载图片与保存数据库 将 ...

  6. Java使用Apache的HttpClient组件发送https请求

    如果我们直接通过普通的方式对https的链接发送请求,会报一个如下的错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Va ...

  7. destoon 短信发送函数及短信接口修改

    // $DT在common.inc.php中定义, $CACHE = cache_read('module.php'); $DT = $CACHE['dt'];  从缓存里读取网站配置信息. //$d ...

  8. JAVA基础篇—文件上传下载

    /index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pa ...

  9. Linux异常体系之stubs_offset

    转自 http://www.xuebuyuan.com/2208550.html 在ARM V4及V4T以后的大部分处理器中,中断向量表的位置可以有两个位置:一个是0x00000000,另一个是0xf ...

  10. 同一条sql在mysql5.6和5.7版本遇到的问题。

    之前用的是mysql 5.6版本,执行select * from table group by colunm 是可以出结果的, 但是切换的5.7版本,这条sql就报错, Expression #1 o ...