hdu 3709 Balanced Number(平衡数)--数位dp
Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 9036 Accepted Submission(s): 4294
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 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).
#include<iostream>
#include<string.h>
#define ll long long
using namespace std;
ll shu[], dp[][][];//dp[i][j][k],i是长度,j是支点,k是力矩和,dp[i][j][k]是以j为支点的平衡数的数量
ll dfs(ll len, ll zhidian, ll sum, bool shangxian)
{
if (len == )
return sum == ? : ;
if (sum < )//因为力矩和是从支点右边开始算的(sum>0),如果左边的力矩都处理完之后sum<0,那一定不是平衡数
return ;
if (!shangxian&&dp[len][zhidian][sum]!=-)
return dp[len][zhidian][sum];
ll mx, cnt = ;
mx = (shangxian ? shu[len] : );
for (ll i = ; i <= mx; i++)//注意是<=
{
ll temp = sum;
temp = temp + (len - zhidian)*i;//len会不断的被return ,细细体会
cnt = cnt + dfs(len - , zhidian, temp, i == mx && shangxian);
}
if (!shangxian)
dp[len][zhidian][sum] = cnt;
return cnt;
} ll solve(ll n)
{
ll len = ;
while (n)
{
shu[++len] = n % ;
n = n / ;
}
ll ans = ;
for (ll i = ; i <= len; i++)//支点是从1开始,因为最高位的数一定不是平衡数
ans = ans + dfs(len, i, , true);
return ans - (len - );//如果0是支点,程序也会判断是平衡数,但是不符合题意
}
int main()
{
ll l, r, t;
scanf("%lld", &t);
memset(dp,-,sizeof(dp));//如果默认0会TLE
while (t--)
{
scanf("%lld%lld", &l, &r);
printf("%lld\n", solve(r) - solve(l - ));
}
return ;
}
hdu 3709 Balanced Number(平衡数)--数位dp的更多相关文章
- HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu 3709 Balanced Number(数位dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:给定区间[a,b],求区间内平衡数的个数.所谓平衡数即有一位做平衡点,左右两边数字的力矩相 ...
- HDU 3709 Balanced Number(数位DP)题解
思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...
- HDU 3709 Balanced Number (数位DP)
题意: 找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数. 思路: 一开始以为需要枚举位数,枚举前缀和,枚举后缀和,一旦枚举起来就会M ...
- HDU - 3709 - Balanced Number(数位DP)
链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...
- hdu3709 (平衡数) 数位DP
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 3709 Balanced Number
发现只要Σa[i]*i%Σa[i]==0就可以. #include<iostream> #include<cstdio> #include<cstring> #in ...
- SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...
随机推荐
- MyBatis 插入主键方式和返回主键
这使用的mysql数据库,下面这种方式(没有给mysql设置自动增长)是插入主键方式: <insert id="insertBook" parameterType=" ...
- [转载]如何将word文档直接发布到新浪博客
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- Redis配置参数汇总
==配置文件全解=== ==基本配置daemonize no 是否以后台进程启动databases 16 创建database的数量(默认选中的是database 0) save 900 1 #刷新快 ...
- leetcode 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: - 左括号必须用相同类型的右括号闭合. - 左括号必须以正确的顺序闭合. 注意空字符 ...
- ASP.NET MVC学习目录
一.ASP.NET MVC原理详解 1.了解MVC架构模式 3.学习ASP.NET MVC的必备语言知识 4.MVC中的razor语法详解 5.ASP.NET MVC路由系统机制详细讲解 6.ASP. ...
- PostMan工具使用之基础篇
PostMan工具使用之基础篇 一.什么是PostMan Postman一款非常流行的API调试工具.(其他测试工具 Jmeter.soapUI) 二.下载及安装: 1.下载: 下载地址:https: ...
- iOS 错误 undefined symbols for architecture i386
undefined symbols for architecture i386 这个错误困扰了我几个小时. 网上很多问这个问题的,回答基本上都是说在 target 里面去的 armv64 什么什么的. ...
- 利用C# CefSharp Python采集某网站简历并自动发送邀请短信
以往爬虫没怎么研究过,最近有个需求,要从某网站采集敏感信息,稍稍考虑了一下,决定利用C# Winform和Python一起来解决这个事件. 整个解决方案不复杂:C#编写WinForm窗体,进行数据分析 ...
- CodeChef April Challenge 2019题解
传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...
- [Objective-C语言教程]复合对象(33)
在Objective-C中,可以在类集群中创建子类,该类集合定义了一个嵌入在其中的类. 这些类对象是复合对象.你可能想知道什么是类集群,下面首先了解什么是类集群. 1. 类集群 类集群是基础框架广泛使 ...