链接:

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)的更多相关文章

  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 5898 odd-even number 数位DP

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

  6. 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 ...

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

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

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

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

  9. HDU 5179 beautiful number 数位dp

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

随机推荐

  1. Hibernate-validator数据验证

    前言 数据效验工作在开发工作中,是非常重要的,保证数据的正确性,可靠性,安全性.不仅在前端进行效验,还要在后台继续进行效验. 前端做验证只是为了用户体验,比如控制按钮的显示隐藏,单页应用的路由跳转等等 ...

  2. GhostScript说明

    关于ghostscript(以下简称gs).Gs是一个地下工作者,一般用户不熟悉它,因为它上不和用户直接打交道,下不直接接触打印机.但是在打印工作中它却扮演了极为重要的解色. 一般从用户常见文件如图片 ...

  3. java笔记4

    private关键字 1.是一个权限修饰符.       2.用于修饰成员       3.被私有化的成员只能在本类中有效 常用之一: -将成员变量私有化,对外提供对应的set,get方法对其进行访问 ...

  4. [洛谷P3227][HNOI2013]切糕

    题目大意:有一个$n\times m$的切糕,每一个位置的高度可以在$[1,k]$之间,每个高度有一个代价,要求四联通的两个格子之间高度最多相差$D$,问可行的最小代价.$n,m,k,D\leqsla ...

  5. go 基本IO接口

    package main import ( "fmt" "io" "strings" ) func ReadFrom(reader io.R ...

  6. oracle数据库 部分函数的用法

    select * from tab; //获取当前用户的数据库的所有表名 select sys_guid(),UserName from TESTLIKUI; //获取guid select sys_ ...

  7. 使用 Code First 数据库迁移

    当 Entity Framework Code First 的数据模型发生改变时,默认会引发一个System.InvalidOperationException 的异常.解决方法是使用DropCrea ...

  8. Flutter Animation AnimatedBuilder

    Flutter AnimatedBuilder 创建动画的widget Key key, @required Listenable animation, @required this.builder, ...

  9. 第三章:JavaScript选择元素

    我们使用jQuery时,很常用的套路是“两步”第一步:选取元素第二步:对选中的元素执行需要的操作这一章我们重点研究第一步,如何使用jQuery选取元素以及对选取的结果进行“各种筛选”以满足我们的需求. ...

  10. 敏感词检测、屏蔽设计(iOS & Android)

    敏感词检测 服务器端最常使用的算法是DFA算法.如果服务器端使用java实现常规的DFA算法,假若... 源码:https://github.com/qiyer/DFA_Cplusplus