【HDU 3709】 Balanced Number (数位DP)
Balanced Number
Problem DescriptionA 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].InputThe 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).OutputFor each case, print the number of balanced numbers in the range [x, y] in a line.Sample Input2
0 9
7604 24324Sample Output10
897AuthorGAO, YuanSource
一开始纠结的地方呢就是一个数可不可能有两个平衡点。。然后就觉得会重复。
用物理的思想想的话好像一个物体又没有两个重心??可是我不会用数学方法严谨证明ORZ。。。
然后就是普通的数位DP,f[i][j][k]表示填i位j是平衡点,然后k是当前的计算值。(不超过4000)
记忆化搜索大法好,又快又好打,特别是多组!!
记得减掉00、000、0000的!
代码如下:
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- #include<cmath>
- using namespace std;
- #define LL long long
- LL f[][][];
- int a[],c;
- LL ffind(int n,int k,int sum,bool flag)
- {
- if(n==) return (sum==);
- if(!flag&&f[n][k][sum]!=-) return f[n][k][sum];
- int ed=flag?a[n]:;
- LL ans=;
- for(int i=;i<=ed;i++)
- ans+=ffind(n-,k,sum+i*(k-n),flag&&(i==ed));
- if(!flag) f[n][k][sum]=ans;
- return ans;
- }
- LL get_ans(LL n)
- {
- if(n==-) return ;
- if(n==) return ;
- c=;
- LL x=n;
- while(x) a[++c]=x%,x/=;
- LL ans=;
- for(int i=;i<=c;i++) ans+=ffind(c,i,,);
- return ans-c+;
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- memset(f,-,sizeof(f));
- while(T--)
- {
- LL x,y;
- scanf("%lld%lld",&x,&y);
- get_ans();
- LL ans=get_ans(y)-get_ans(x-);
- printf("%lld\n",ans);
- }
- return ;
- }
[HDU 3709]
感觉我没有判负就A了?!!
2016-10-08 20:31:48
【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 - 3709 - Balanced Number(数位DP)
链接: https://vjudge.net/problem/HDU-3709 题意: A balanced number is a non-negative integer that can be ...
- 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 ...
随机推荐
- 一种协程的 C/C++ 实现
一种协程的 C/C++ 实现 介绍 在前几天接触到了协程的概念,觉得很有趣.因为我可以使用一个线程来实现一个类似多线程的程序,如果使用协程来替代线程,就可以省去很多原子操作和内存栅栏的麻烦,大大减少与 ...
- ArrayList的深度copy和浅度拷贝
ArrayList的浅度拷贝方式: 通过Collections.copy方法实现浅度拷贝 ArrayList<GuideGroup> questionGuideGroupList = ne ...
- 20151214 jquery插件代码备份
;(function ($) { //局部性的 /*$.fn.extend({ 'nav' : function (color) { $(this).find('.nav').css({ 'list- ...
- 多个线程访问url
多个线程访问url package com.aig.ecompassios.ecard; import java.io.BufferedReader; import java.io.InputStre ...
- myeclipse启动报JVM terminated. Exit code=1
报错信息如图: 解决办法: 删除当前workspaces下文件夹,路径为:%Workspaces%/.metadata/.plugins/org.eclipse.core.runtime
- 关于C++几个容易混淆的概念总结
1.函数指针和指针函数 函数指针说的就是一个指针,但这个指针指向的函数,不是普通的基本数据类型或者类对象.函数指针的定义: int (*func)(int a, int b); // 声明函数指针 指 ...
- c++ primer复习(四)
1 标准库容器 顺序容器:vector.list.deque 容器适配器:stack.queue.priority_queue 2 容器元素类型约束: 容器元素类型必须支持复制和赋值,因为容器存放的都 ...
- (转)Libevent(1)— 简介、编译、配置
转自:http://name5566.com/4190.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...
- 基于NodeJs的网页爬虫的构建(一)
好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙.接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS. 背 ...
- C# 给picturebox添加滚动条
在一个项目中需要给picturebox增加滚动条,我先前的做法和网上一样,将picturebox放在一个panel上,将panel的AutoScroll设置为ture,将picturebox的Size ...