Balanced Number

Problem Description
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
Author
GAO, Yuan
Source
 
 
【题意】
 
    一个数n是平衡数当且仅当选一个位置j时,对于每一位(i-j)*a[i]的和为0,例如4139,4921,求l到r中有多少个平衡数(l<=r<=2^63-1)多组数据
 
【分析】
 
  

  一开始纠结的地方呢就是一个数可不可能有两个平衡点。。然后就觉得会重复。
  用物理的思想想的话好像一个物体又没有两个重心??可是我不会用数学方法严谨证明ORZ。。。
  然后就是普通的数位DP,f[i][j][k]表示填i位j是平衡点,然后k是当前的计算值。(不超过4000)
  记忆化搜索大法好,又快又好打,特别是多组!!

  记得减掉00、000、0000的!

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long LL f[][][];
int a[],c; LL ffind(int n,int k,int sum,bool flag)
{
if(n==) return (sum==);
if(!flag&&f[n][k][sum]!=-) return f[n][k][sum];
int ed=flag?a[n]:;
LL ans=;
for(int i=;i<=ed;i++)
ans+=ffind(n-,k,sum+i*(k-n),flag&&(i==ed));
if(!flag) f[n][k][sum]=ans;
return ans;
} LL get_ans(LL n)
{
if(n==-) return ;
if(n==) return ;
c=;
LL x=n;
while(x) a[++c]=x%,x/=;
LL ans=;
for(int i=;i<=c;i++) ans+=ffind(c,i,,);
return ans-c+;
} int main()
{
int T;
scanf("%d",&T);
memset(f,-,sizeof(f));
while(T--)
{
LL x,y;
scanf("%lld%lld",&x,&y);
get_ans();
LL ans=get_ans(y)-get_ans(x-);
printf("%lld\n",ans);
}
return ;
}

[HDU 3709]

感觉我没有判负就A了?!!

2016-10-08 20:31:48

【HDU 3709】 Balanced Number (数位DP)的更多相关文章

  1. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  2. hdu 3709 Balanced Number(平衡数)--数位dp

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  3. HDU 3709 Balanced Number(数位DP)题解

    思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...

  4. HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)

    平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...

  5. HDU - 3709 - Balanced Number(数位DP)

    链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...

  6. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  7. HDU 5787 K-wolf Number 数位DP

    K-wolf Number Problem Description   Alice thinks an integer x is a K-wolf number, if every K adjacen ...

  8. hdu3709 Balanced Number (数位dp+bfs)

    Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...

  9. HDU3709:Balanced Number(数位DP+记忆化DFS)

    Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...

  10. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

随机推荐

  1. 从键盘输入当月利润I,求应发放奖金总数?

    企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%:20万到40万之间时 ...

  2. jquery登录验证插件

    最近写了个登录验证的jquery插件,其中功能还不是很完善,需要进一步改进,先放出来看看先. /** * 功能描述:本模块实现普通网站登录验证,以后可以添加二维码验证,以及第三方帐号登录验证 * 关联 ...

  3. Scala可变参数列表,命名参数和参数缺省

    重复参数 Scala在定义函数时允许指定最后一个参数可以重复(变长参数),从而允许函数调用者使用变长参数列表来调用该函数,Scala中使用“*”来指明该参数为重复参数.例如: scala> de ...

  4. js高程 第 4章 变量、作用域和内存问题 【笔记】

    4.4 小结 JavaScript变量可以用来保存两种类型的值:基本类型值和引用类型值.基本类型的值源自以下 5 种基本数据类型:Undefined.Null.Boolean.Number 和 Str ...

  5. 在HTML中怎么去掉a标签(超链接)的下划线?

    <style type="text/css">a:link,a:visited{ text-decoration:none; /*超链接无下划线*/}a:hover{ ...

  6. 【转】【SQLServer】SQL事务用法begin tran,commit tran和rollback tran的用法

    Sql Server 2005/2008中提供了begin tran,commit tran和rollback tran来使用事务.begin tran表示开始事务, commit tran表示提交事 ...

  7. SQL Server自动化运维系列 - 多服务器数据收集和性能监控

    需求描述 在生产环境中,很多情况下需要采集数据,用以定位问题或者形成基线. 关于SQL Server中的数据采集有着很多种的解决思路,可以采用Trace.Profile.SQLdiag.扩展事件等诸多 ...

  8. C语言——N个人围成一圈报数淘汰问题

    <一>问题描述: 有17个人围成一圈(编号为0-16),从第 0号的人开始从 1报数, 凡报到 3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止. 问此人原来的位置是多少号? ...

  9. UVA 11729 - Commando War(贪心 相邻交换法)

    Commando War There is a war and it doesn't look very promising for your country. Now it's time to ac ...

  10. SGU 260.Puzzle (异或高斯消元)

    题意: 有n(<200)个格子,只有黑白两种颜色.可以通过操作一个格子改变它和其它一些格子的颜色.给出改变的关系和n个格子的初始颜色,输出一种操作方案使所有格子的颜色相同. Solution: ...