HDU3709 Balanced Number (数位dp)
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 [ x, y].
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 [x, y] 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)的更多相关文章
- HDU3709 Balanced Number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3709 Balanced Number Time Limit: 10000/5000 MS (Java/Others) ...
- 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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题目大意就是求给定区间内的平衡数的个数 要明白一点:对于一个给定的数,假设其位数为n,那么可以有 ...
- 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 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- hdu3709 Balanced Number 树形dp
A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. ...
- Balanced Number 数位dp
题意: 给出求ab之间有多少个平衡数 4139为平衡数 以3为轴 1*1+4*2==9*1 思路很好想但是一直wa : 注意要减去前导零的情况 0 00 000 0000 不能反复计 ...
- [HDU3709]Balanced Number
[HDU3709]Balanced Number 试题描述 A balanced number is a non-negative integer that can be balanced if a ...
- 多校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 用作标记,当现在枚举的数小 ...
随机推荐
- startDiscovery() and startLeScan().
You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetoot ...
- 原生js实现仿window10系统日历效果
舞动的灵魂版js日历,完全采用js实现,故而实现了与语言无关,jsp.asp.net php asp均可使用.无论你是开发软件,还是网站,均是不可或缺的实用代码. 该日历主要实现了获取当前时间时分秒, ...
- Android开发应用异步检查更新代码
开发环境:android studio sdk 4.0及以上 场景:用户点击检查更新按钮进行检查服务器版本号,若有新版本则进行下载更新.异步检测版本号 package com.example.q ...
- 所有的代码生成器都是浮云,如果可以用aspx文件作为模板
首先申明:标题中的如果是可以去掉的. 想写这篇文章很长时间了,一来是跟大家分享一下,别浪费时间在写代码生成器上面了,什么CodeSmith,XXCodeGenerator等等,都是浮云:二来想跟大家交 ...
- SVN提交错误:working copy is not up-to-date解决方法
我在项目中删了2个jar,然后SVN提交,一直提交不成功 svn在提交时报错如下图: working copy is not up-to-date svn:commit failed(details ...
- 使用highcharts 绘制Web图表
问题描述: 使用highcharts 绘制Web图表 Highcharts说明: 问题解决: (1)安装Highcharts 在这些图表中,数据源是一个典型的JavaScrip ...
- Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- eclipse配置mahout
1.在elcipse上建立一个java project 项目名:mymahout 2.建立libs文件夹,在mahout 0.9的lib文件夹下找到一下java包 其中log4j.properties ...
- Struts 2知识回顾----拦截器(Intercept)总结
什么是Struts 2拦截器? 从软件构架上来说,拦截器是实现了面向方面编程的组件.它将影响了多个业务对象的公共行为封装到一个个可重用的模块,减少了系统的重复代码,实现功能的高度内聚,确保了业务对象的 ...
- Android 一个Activity保存它自己的实例
一个Activity保存他自己的实例的作用是,在其他Activity中可以方便的调用该Activity里的方法. 我们可以使用一个静态的变量保存当前Activity的实例,并将其标志为private访 ...