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

  1. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  2. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  3. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  6. 【HDU 3709】 Balanced Number (数位DP)

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

  7. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  8. 【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 ...

  9. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

随机推荐

  1. 2017-2018-4 20155203《网络对抗技术》Exp3 免杀原理与实践

    1.基础问题回答 (1)杀软是如何检测出恶意代码的? 分析恶意程序的行为特征,分析其代码流将其性质归类于恶意代码 (2)免杀是做什么? 使恶意代码避免被查杀,也就是要掩盖恶意代码的特征 (3)免杀的基 ...

  2. 汇编 fsub ,fmul,fdiv,fild,CVTTPS2PI 指令

    知识点:  浮点指令 fsub 一.浮点指令fsub 格式 fsub memvar // st0=st0-memvar 知识点:  浮点指令 fmul 一.浮点指令fmul 格式 fmul mem ...

  3. 【个人】爬虫实践,利用xpath方式爬取数据之爬取虾米音乐排行榜

    实验网站:虾米音乐排行榜 网站地址:http://www.xiami.com/chart  难度系数:★☆☆☆☆ 依赖库:request.lxml的etree (安装lxml:pip install ...

  4. .netCoreMVC添加数据仓储

    在上一篇关于CodeFirst从零搭建ASP.NETCore2.0中搭建起了完整.netCoreMVC项目,在这一篇中将实现如何注册service服务和Repository数据仓储到web中实现数据的 ...

  5. Asp.Net_抓包解析xml文件为json

    protected void Button1_Click(object sender, EventArgs e) { string Phone = this.Txt_Con.Text; string ...

  6. HTML 表格实例

    1.表格这个例子演示如何在 HTML 文档中创建表格. <p>每个表格由 table 标签开始.</p><p>每个表格行由 tr 标签开始.</p>&l ...

  7. LintCode——全排列

    描述:给定一个数字列表,返回其所有可能的排列. 样例:给出一个列表[1,2,3],其全排列为:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] 说明: ...

  8. UI Recorder 安装教程(二)

    前言: UI Recorder支持无线native app(Android, iOS)录制, 基于macaca实现:https://macacajs.com/ 本次教程只针对无线native app( ...

  9. 关于maven:调整你的maven的jdk版本为 xxxx

    找到你的.m2文件 在里面添加一下信息 实例  将其更改成1.7 <profiles> <profile> <id>jdk-1.7</id> <! ...

  10. 四则运算APP,团队项目之需求

    队名:IG.Super 成员:范铭祥,曾威,刘恒,黄伟俊. 一.程序功能需求 程序可以出带括号的正整数四则运算,支持分数,除法保留两位小数,如:(1/3+1)*2 = 2.67,特别注意:这里是2.6 ...