HDU - 3709 - Balanced Number(数位DP)
链接:
https://vjudge.net/problem/HDU-3709
题意:
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 42 + 11 = 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].
思路:
刚开始没算具体大小。。以为直接算会暴空间。
记录当前的力矩和,让高位表示正数,这样在从高位到低位的时候,如果力矩边成负值,可以直接返回0,因为低位的权值是负的。
同时要枚举每一个位置作为支点的情况,再去减掉每次算的0.
代码:
// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;
LL F[20][20][1600];
LL dig[20];
LL a, b;
LL Dfs(int pos, int piv, int sum, bool lim)
{
if (pos == -1)
return sum == 0;
if (sum < 0)
return 0;
if (!lim && F[pos][piv][sum] != -1)
return F[pos][piv][sum];
int up = lim ? dig[pos] : 9;
LL ans = 0;
for (int i = 0;i <= up;i++)
ans += Dfs(pos-1, piv, (pos-piv)*i+sum, lim && i == up);
if (!lim)
F[pos][piv][sum] = ans;
return ans;
}
LL Solve(LL x)
{
int p = 0;
while(x)
{
dig[p++] = x%10;
x /= 10;
}
LL ans = 0;
for (int i = 0;i < p;i++)
ans += Dfs(p-1, i, 0, true);
return ans-(p-1);
}
int main()
{
// freopen("test.in", "r", stdin);
memset(F, -1, sizeof(F));
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lld%lld", &a, &b);
printf("%lld\n", Solve(b)-Solve(a-1));
}
return 0;
}
HDU - 3709 - Balanced Number(数位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
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 3709 Balanced Number(数位DP)题解
思路: 之前想直接开左右两边的数结果爆内存... 枚举每次pivot的位置,然后数位DP,如果sum<0返回0,因为已经小于零说明已经到了pivot右边,继续dfs只会越来越小,且dp数组会炸 ...
- HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- 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 ...
- hdu3709 Balanced Number (数位dp+bfs)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- HDU3709:Balanced Number(数位DP+记忆化DFS)
Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
随机推荐
- HDFS面试题
hadoop节点动态上线下线怎么操作? )节点上线操作: 当要新上线数据节点的时候,需要把数据节点的名字追加在 dfs.hosts 文件中 ()关闭新增节点的防火墙 ()在 NameNode 节点的 ...
- spring框架学习(二)——注解方式IOC/DI
什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...
- scrapy服务化持久运行
如果要将scrapy做成服务持久运行,通常我们会尝试下面的方式,这样是不可行的: class myspider(scrapy.Spider): q = queue() #task qu ...
- LeetCode第151场周赛(Java)
这是我第一次写周赛的题目,而且还是虚拟的.从这次起,以后就将所有错过的题目都写到博客来.当然既然是我错的,那代码肯定不是我自己的.我会注明来源.并且我会自己敲一遍.多总结总是没坏处的. 另外比较糟糕的 ...
- quartz2.3.0(八)使用日历排除不应该执行任务的时间段
Job任务类 package org.quartz.examples.example8; import java.util.Date; import org.slf4j.Logger; import ...
- 获取SpringMVC中所有RequestMapping映射URL信息
SpringMVC启动的时候,会把接口信息收集在RequestMappingHandlerMapping中,故可以通过这个类,拿到全部的映射信息,Sample代码段如下: @Autowired pri ...
- Java Web 深入分析(11) JVM 体系结构与工作方式
jvm体系 jvm简介 java virtual machine jvm体系详解 jvm工作机制 虚拟机怎么执行代码 jvm为何基于栈 执行引擎 执行引擎过程 java调用栈 总结
- vue 自定义image组件
介绍 1:当图片加载失败时,给出错误提示. 2:当图片加载中时,给出加载提示. 3:图片处理模式:等比缩放/裁剪/填充/... 1.图片加载状态处理 通过给图片绑定load事件与error事件处理函数 ...
- canvas炫酷时钟
canvas炫酷时钟 实现的功能 主要用到canvas的一些基础api 直接看效果 html: <canvas id="myCanvas" width="500&q ...
- Ceph集群部署(基于Luminous版)
环境 两个节点:ceph0.ceph1 ceph0: mon.a.mds.mgr.osd.0.osd.1 ceph1: mon.b.osd.2.osd.3 操作系统:ubuntu14.04 网络配置: ...