Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2945    Accepted Submission(s): 1348

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
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3711 3715 3718 3713 3712
枚举中心点,然后按位枚举,长度由len到0,算出当前权值之和pre,然后结束时判断pre值是否为0,为0,返回1,否则返回1。
用dp[len][cen][pre]保存长度为i,中心点为k,之前的权值之和为pre的数字有多少种的状态。
最后需要去除全是0的情况,因为全是0的话,中心点可随意枚举没,所以需要减去(len-1)。
 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
#define LL long long
#define maxn 30
LL dp[maxn][maxn][]; // dp[i][k][j]代表长度为i,中心点为k,最高位到与最高位距离为i的位置经过的数字的权值之和j的数字有多少种
LL digit[maxn];
__int64 dfs(int len,int cen,int pre,bool fp) //dfs版本的纯属暴力枚举每一个数字,而递推版本的是考虑了前缀的影响
{
if(len==)
return pre==;
if(pre<)
return ;
if(!fp && dp[len][cen][pre] != -) //
{
return dp[len][cen][pre];
}
LL ret =;
int fpmax = fp ? digit[len] : ;
for(int i=;i<=fpmax;i++) //分别算出以i开头的数的方案数,
{
LL temp=dfs(len-,cen,pre+i*(len-cen),fp && i == fpmax);
ret+=temp;
}
if(!fp)
dp[len][cen][pre]= ret;
return ret;
} LL f(LL n)
{
if(n==-)
return ;
int len=;
while(n)
{
digit[++len] = n % ;
n /= ;
}
LL ans=;
for(int cen=;cen<=len;cen++)
{
ans+=dfs(len,cen,,true);
}
return ans-len+;
}
void init()
{
memset(dp,-,sizeof(dp));
}
int main()
{
// freopen("test.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
init();
LL n,m;
scanf("%I64d%I64d",&n,&m);
LL ans1=f(m);
// init();
LL ans2=f(n-);
printf("%I64d\n",ans1-ans2);
}
return ;
}

hdu3709 (平衡数) 数位DP的更多相关文章

  1. HDU3709 Balanced Number —— 数位DP

    题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others)     ...

  2. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  3. 【BZOJ-1026】windy数 数位DP

    1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5230  Solved: 2353[Submit][Sta ...

  4. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  5. bzoj 1026 [SCOI2009]windy数 数位dp

    1026: [SCOI2009]windy数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...

  6. hdu3709 Balanced Number 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...

  7. luogu P2657 [SCOI2009]windy数 数位dp 记忆化搜索

    题目链接 luogu P2657 [SCOI2009]windy数 题解 我有了一种所有数位dp都能用记忆话搜索水的错觉 代码 #include<cstdio> #include<a ...

  8. 【BZOJ 3326】[Scoi2013]数数 数位dp+矩阵乘法优化

    挺好的数位dp……先说一下我个人的做法:经过观察,发现这题按照以往的思路从后往前递增,不怎么好推,然后我就大胆猜想,从前往后推,发现很好推啊,维护四个变量,从开始位置到现在有了i个数 f[i]:所有数 ...

  9. 洛谷P2657 [SCOI2009]windy数 [数位DP,记忆化搜索]

    题目传送门 windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个win ...

随机推荐

  1. 【计算几何】FZU Problem 2270 Two Triangles

    http://acm.fzu.edu.cn/problem.php?pid=2270 [题意] 给定6到10个点,从中选出6个不同的点组成两个三角形,使其中一个三角形可以通过另一个三角形平移和旋转得到 ...

  2. idea添加虚拟参数

    1. 2.

  3. 2016 Multi-University Training Contest 8 solutions BY 学军中学

    1001: 假设有4个红球,初始时从左到右标为1,2,3,4.那么肯定存在一种方案,使得最后结束时红球的顺序没有改变,也是1,2,3,4. 那么就可以把同色球都写成若干个不同色球了.所以现在共有n个颜 ...

  4. 【2018 Multi-University Training Contest 1】

    01:https://www.cnblogs.com/myx12345/p/9362221.html 02:https://www.cnblogs.com/myx12345/p/9382267.htm ...

  5. lightoj 1293 - Document Analyzer [ 两指针 + 字符串 ]

    传送门 1293 - Document Analyzer   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: ...

  6. 由八数码问题引入。对BFS有更深考虑

    12号到今天共研究八数码问题poj1077,首先用的是普通BFS,遇到很多问题,开始用一个二级指针作为结构成员,知道了二级指针与二维数值名的不同!http://write.blog.csdn.net/ ...

  7. python学习之 - re模块

    re模块功能:实现字符串匹配. 元字符 描述\ 将下一个字符标记符.或一个向后引用.或一个八进制转义符.例如,“\\n”匹配\n.“\n”匹配换行符.序列“\\”匹配“\”而“\(”则匹配“(”.即相 ...

  8. 原生js中stopPropagation,preventDefault,return false的区别

    1.stopPropagation:阻止事件的冒泡,但不阻止事件的默认行为. 最好莫过于用例子说明: <div id='div'  onclick='alert("div") ...

  9. Visual studio 2017 中的Javascript智能提示与调试

    1.智能提示 对于JS文件中的API,你若需要让那个JS文件中的方法能够在你写的那个JS文件中能够智能显示的话,直接把它拉进你的JS文件中就好了. 举个例子:你想 在你正在写的a.js文件中引用b.j ...

  10. jenkins的构建日志(console output)分类解析

    每个jenkins的job构建过程中会产生大量日志,如何快速找到或者查看我们关心的日志显得很有意义,为此jenkins提供了一个插件“Log Parser Plugin”可以帮助我们完成这个任务. 1 ...