博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu3709——Balanced Number(数位dp)
阅读量:2343 次
发布时间:2019-05-10

本文共 1846 字,大约阅读时间需要 6 分钟。

Problem 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

s表示支点,sum表示计算到pos位时的值

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 10000000005#define Mod 10001using namespace std;int dight[30];long long dp[20][20][5000];long long dfs(int pos,int s,bool limit,int sum){ if(pos==0) return sum==0; if(!limit&&dp[pos][s][sum]!=-1) return dp[pos][s][sum]; int end; long long ret=0; if(limit) end=dight[pos]; else end=9; for(int d=0;d<=end;++d) { ret+=dfs(pos-1,s,limit&&d==end,sum+(pos-s)*d); } if(!limit) dp[pos][s][sum]=ret; return ret;}long long solve(long long a){ memset(dight,0,sizeof(dight)); int cnt=1; while(a!=0) { dight[cnt++]=a%10; a/=10; } long long ans=0; for(int i=1;i

转载地址:http://yvcvb.baihongyu.com/

你可能感兴趣的文章
Java NIO(六)SocketChannel、ServerSocketChannel
查看>>
6 Netty 架构剖析
查看>>
Netty简介
查看>>
Redis,API的理解和使用-全局命令
查看>>
shell之eval
查看>>
postgresql基本操作
查看>>
SQLAlchemy使用
查看>>
word设置标题
查看>>
git之HEAD
查看>>
基于2.6内核的Init_task进程之一
查看>>
C代码插入汇编
查看>>
C++基础知识-之强指针(韦东山视频学习)
查看>>
C++之Android弱指针
查看>>
C++基础知识之vector和[=] [&] [=,&]拷贝
查看>>
C语言常见错误
查看>>
Init中的next_token()函数
查看>>
STL之MAP和Vector
查看>>
智能指针 unique_ptr
查看>>
Init.rc配置文件Action字段解析
查看>>
uml问题解决
查看>>