HDU3709 Balanced Number (数位dp)

时间:2021-03-15 10:36:02
 Balanced Number

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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 [ xy].

 

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 [xy] in a line.

Sample Input

2
0 9
7604 24324

Sample Output

10
897

数位dp

表示暂时还不明白为什么这样搜索不会超时。。

#include<cstdio>
#include<cstring>
long long f[][][];
int a[];
long long dfs(int pos,int z,int l,bool pd){
if(pos<=) return l==;
if(l<) return ;
if(!pd&&f[pos][z][l]!=-) return f[pos][z][l];
int i,j,k,en;
en=pd?a[pos]:;
long long ans=;
for(i=;i<=en;i++)
ans+=dfs(pos-,z,l+(pos-z)*i,pd&&i==en);
if(!pd) f[pos][z][l]=ans;
return ans;
}
long long sum(long long x){
int i,n=;
while(x){
a[++n]=x%;
x=x/;
}
long long ans=;
for(i=n;i>;i--)
ans+=dfs(n,i,,);
return ans-n+;
}
int main()
{
int tt;
long long x,y;
scanf("%d",&tt);
while(tt--){
scanf("%lld%lld",&x,&y);
memset(f,-,sizeof(f));
printf("%lld\n",sum(y)-sum(x-));
}
return ;
}

HDU3709 Balanced Number (数位dp)的更多相关文章

  1. HDU3709 Balanced Number —— 数位DP

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

  2. hdu3709 Balanced Number &lpar;数位dp&plus;bfs&rpar;

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

  3. hdu3709 Balanced Number 数位DP

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

  4. HDU3709:Balanced Number&lpar;数位DP&plus;记忆化DFS&rpar;

    Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is p ...

  5. HDU 3709 Balanced Number &lpar;数位DP&rpar;

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  6. hdu3709 Balanced Number 树形dp

    A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...

  7. Balanced Number 数位dp

    题意: 给出求ab之间有多少个平衡数   4139为平衡数   以3为轴   1*1+4*2==9*1 思路很好想但是一直wa  : 注意要减去前导零的情况 0 00 000 0000   不能反复计 ...

  8. &lbrack;HDU3709&rsqb;Balanced Number

    [HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...

  9. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

随机推荐

  1. Spark RDD到底是个什么东西

    前言 用Spark有一段时间了,但是感觉还是停留在表面,对于Spark的RDD的理解还是停留在概念上,即只知道它是个弹性分布式数据集,其他的一概不知 有点略显惭愧.下面记录下我对RDD的新的理解. 官 ...

  2. SolrCloud-5&period;2&period;1 集群部署及测试

    一. 说明 Solr5内置了Jetty服务,所以不用安装部署到Tomcat了,网上部署Tomcat的资料太泛滥了. 部署前的准备工作: 1. 将各主机IP配置为静态IP(保证各主机可以正常通信,为避免 ...

  3. how to monitor system logs and export to files simultaneously

    What will you do when you conduct a malware analysis on a smartphone? You will focus on running proc ...

  4. &lbrack;sql server、oracle&rsqb; 分组取最大值最小值常用sql

    sqlserver2005前: --分组取最大最小常用sql--测试环境if OBJECT_ID('tb') is not null drop table tb;gocreate table tb(  ...

  5. App Store生存指南

    资格获取   如果已经有App Store开发帐号请跳过此节.   App Store的资格获取其实一直以来都不算难,和其它事情一样,需要的只是耐心.现在苹果对申请者的文书手续要求已经比几年前简化多了 ...

  6. sqlite语句主页

    因为现在android手机用sqlite数据,但是sql语句很多和sqlserver不同..所以还是把官网记下以便开发:http://www.sqlite.org/lang.html

  7. js layer&period;js

    var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); 关闭弹出页 全屏弹出页面 layer.o ...

  8. Python设计模式 - 基础 - 封装 &amp&semi; 继承 &amp&semi; 多态

    面向对象的核心是对象,世间万物都可以看作对象,任何一个对象都可以通过一系列属性和行为来描述,可以包含任意数量和类型的数据或操作.类是用来描述具有相同属性和方法的所有对象的集合.类通常是抽象化的概念,而 ...

  9. magento如何安装语言包

    1,先下安装,直接在www.magento.com(搜索chinese)官网获得下载密钥,然后在下载站点输入密钥就可以下载,下载完成后的安装包放到app/local文件夹下即可,到后台刷新一下: 2线 ...

  10. asterisk channel driver dev ref

    入口函数load_module    load_config    ast_channel_register console_tech    ast_cli_register_multiple     ...