hdu3709 (平衡数) 数位DP
Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2945 Accepted Submission(s): 1348
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).
0 9
7604 24324
897
#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的更多相关文章
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- 【BZOJ-1026】windy数 数位DP
1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5230 Solved: 2353[Submit][Sta ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- bzoj 1026 [SCOI2009]windy数 数位dp
1026: [SCOI2009]windy数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...
- hdu3709 Balanced Number 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...
- luogu P2657 [SCOI2009]windy数 数位dp 记忆化搜索
题目链接 luogu P2657 [SCOI2009]windy数 题解 我有了一种所有数位dp都能用记忆话搜索水的错觉 代码 #include<cstdio> #include<a ...
- 【BZOJ 3326】[Scoi2013]数数 数位dp+矩阵乘法优化
挺好的数位dp……先说一下我个人的做法:经过观察,发现这题按照以往的思路从后往前递增,不怎么好推,然后我就大胆猜想,从前往后推,发现很好推啊,维护四个变量,从开始位置到现在有了i个数 f[i]:所有数 ...
- 洛谷P2657 [SCOI2009]windy数 [数位DP,记忆化搜索]
题目传送门 windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个win ...
随机推荐
- kafka直连方式消费多个topic
一个消费者组可以消费多个topic,以前写过一篇一个消费者消费一个topic的,这次的是一个消费者组通过直连方式消费多个topic,做了小测试,结果是正确的,通过查看zookeeper的客户端,zoo ...
- POJ 1386 单词接龙问题
题目大意: 给一堆字母,让它们进行接龙,要头对尾能够接的上,问有没有一种方法让所有成语都完成接龙 这道题实际上是在考虑是否存在一条欧拉通路,每个单词产生一条有向线段,由第一个字母指向最后一个字母 这道 ...
- HDU 3397 双lazy标记的问题
题目大意 对一个只有0和1的序列,支持以下几种操作1.将区间所有的值变成12.将区间所有的值变为03.将区间的0和1翻转(0变成1 1变成0)4.求区间中1的个数5.求区间连续最长的1的个数 http ...
- hdu4714树形DP+贪心(乱搞)
Tree2cycle A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...
- [转]Fedora22添加国内软件源和本地软件源
Fedora22添加国内软件源和本地软件源 Linux系统和Windows系统一个很大的区别就是软件安装方式,windows系统下安软件,我们去相应的网站下载软件安装包离线安装就可以了.虽然Linux ...
- BZOJ1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名
n<=1000头牛各有一个未知值Ai,已知m<=10000条形如Ax>Ay的不等关系,求将整个序列排序的最少比较次数. Aa>Ab,Ab>Ac -------> A ...
- win10 笔记本猎豹WiFi无法打开
网卡驱动太新了,先把网卡驱动卸载,重新安装一个就可以,用驱动精灵,17.15.0.5版本就可以
- Spring基于Java的JSR-250注解
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-jsr250-annot ...
- 搭建网络svn实战
工作中的问题(7) 转自:http://blog.csdn.net/xiaoting451292510/article/details/8562570 经常性我们和朋友写一些程序,大家在不同的城市确有 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...