【hdu3709】 Balanced Number
http://acm.hdu.edu.cn/showproblem.php?pid=3709 (题目链接)
题意
求范围${[a,b]}$之间的平衡数的个数,所谓平衡数就是以某一位为支点,两侧的力矩相等。
Solution
数位dp记忆化搜索。
一般的数位dp记忆化搜索一定是从高位往低位搜索,传递2个参数:pos,当前dp的位置;lim是否有上限。如果没有上限就可以利用记忆化的${f}$来返回值了。
这道题的话就是枚举支点,然后从高位往低位搜索过去,复杂度大概是${O(20*10*2000)}$,还有多组数据,记忆化了不虚。
细节
LL
代码
// hdu3709
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>
#define LL long long
#define inf (1ll<<30)
#define MOD 1004535809
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; LL f[20][20][2000],l,r;
int t[20],n; LL dfs(int pos,int o,int val,int lim) { //pos当前位置,o支点,val力矩,lim是否有上限
if (pos==0) return val==0; //已经dp完成
if (val<0) return 0; //力矩已经<0
if (!lim && f[pos][o][val]!=-1) return f[pos][o][val]; //已经搜索过了
LL ans=0;
int end=lim ? t[pos] : 9; //设置枚举上界
for (int i=0;i<=end;i++)
ans+=dfs(pos-1,o,val+(pos-o)*i,lim && i==end);
if (!lim) f[pos][o][val]=ans;
return ans;
}
LL solve(LL p) {
for (n=0;p;p/=10) t[++n]=p%10;
LL ans=0;
for (int i=1;i<=n;i++) ans+=dfs(n,i,0,1);
return ans-(n-1);
}
int main() {
int T;scanf("%d",&T);
memset(f,-1,sizeof(f));
while (T--) {
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-1));
}
return 0;
}
【hdu3709】 Balanced Number的更多相关文章
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【HDU 3709】 Balanced Number (数位DP)
Balanced Number Problem Description A balanced number is a non-negative integer that can be balanced ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)
The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...
- 【leetcode78】Single Number II
题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...
随机推荐
- 2017-2018-2 20155203《网络对抗技术》Exp2 后门原理与实践
基础问题回答 (1)例举你能想到的一个后门进入到你系统中的可能方式? 通过下载带后门的程序或者点开带有后门的软件,或者后门本身就在我的系统里: (2)例举你知道的后门如何启动起来(win及linux) ...
- jQuery调用Asp.Net后台方法
常用的ajax就不讲了,这里主要是说通过ajax调用asp.net后台的cs文件暴露的方法. 前台: <%@ Page Language="C#" AutoEventWire ...
- 【转载】WINAPI宏
原文:http://blog.sina.com.cn/s/blog_3f27dee60100qi4j.html 一直搞不懂为什么在函数前面加上WINAPI.CALLBACK等是什么意思 又不是返回值 ...
- CS100.1x-lab3_text_analysis_and_entity_resolution_student
这次作业叫Text Analysis and Entity Resolution,比前几次作业难度要大很多.相关ipynb文件见我github. 实体解析在数据清洗和数据整合中是一个很重要,且有难度的 ...
- linux中wget的使用方法介绍
wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能和特点:(1)支持断点下传功能:这一点,也是网络蚂蚁和Fl ...
- flask之jinjia2模板(二)
1.1.模板传参 (1)主程序 from flask import Flask,render_template app = Flask(__name__) @app.route('/') def he ...
- JS基础内容小结(event 鼠标键盘事件)(三)
var ev=ev||event 兼容性处理 得到焦点为 onfocus 失去焦点 onblur return false能阻止部分函数的执行 obj.select 选择指定元素里的文本内容 ———— ...
- 【容器魔方解读】AWS Re:Invent 2018大会
每年云计算领域技术与商业风向标之一的AWS Re:Invent大会上周在美国拉斯维加斯召开,如往届一样,AWS密集发布了上百项的新产品或新技术.随着国内近两年云计算尤其是公有云的普及度越来越高,国内各 ...
- 重磅发布丨乐维监控:全面兼容云平台,助力企业DevOps转型升级!
2019年伊始,我们迎来了乐维监控的又一重大功能更新——云平台监控,这将有效帮助企业将云上.云下数据聚合,方便统一化的监控管理与维护!未来,乐维监控每一次的产品功能及版本更新,我们都将第一时间于此发布 ...
- PTA (Advanced Level) 1001 A+B Format
1001 A+B Format Calculate a+b and output the sum in standard format -- that is, the digits must be s ...