HDU - 3709 Balanced Number(数论+数位dp)
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
题目大意:要你找到一个区间的平衡数的数量总和,而平衡数的定义为:对于一个它的特定的十进制数位,其左右两边的数位与特定数位的距离的乘积之和相等(即为杠杆平衡)。
题解:根据平衡数的定义可以推出,对于一个n+1位的不为0的十进制数
(
a
n
a
n
−
1
⋯
a
1
a
0
)
10
(a_na_{n-1}\cdots a_1a_0)_{10}
(anan−1⋯a1a0)10,如果存在整数
i
(
0
≤
i
≤
n
)
i(0\le i\le n)
i(0≤i≤n),使得
a
n
∗
(
n
−
i
)
+
a
n
−
1
∗
(
n
−
1
−
i
)
+
⋯
+
a
i
+
1
∗
1
=
a
i
−
1
∗
1
+
⋯
+
a
1
∗
(
i
−
1
)
+
a
0
∗
i
a_n*(n-i)+a_{n-1}*(n-1-i)+\cdots +a_{i+1}*1=a_{i-1}*1+\cdots +a_1*(i-1)+a_0*i
an∗(n−i)+an−1∗(n−1−i)+⋯+ai+1∗1=ai−1∗1+⋯+a1∗(i−1)+a0∗i,那么我们有
(
a
n
+
a
n
−
1
+
⋯
+
a
1
+
a
0
)
∣
(
a
n
∗
(
n
+
1
)
+
a
n
−
1
∗
n
+
⋯
+
a
1
∗
2
+
a
0
∗
1
)
(a_n+a_{n-1}+\cdots +a_1+a_0)\mid (a_n*(n+1)+a_{n-1}*n+\cdots+a_1*2+a_0*1)
(an+an−1+⋯+a1+a0)∣(an∗(n+1)+an−1∗n+⋯+a1∗2+a0∗1)。记第pos位时,pre=
a
n
∗
(
n
−
p
o
s
)
+
a
n
−
1
∗
(
n
−
p
o
s
−
1
)
+
⋯
+
a
p
o
s
+
1
a_n*(n-pos)+a_{n-1}*(n-pos-1)+\cdots+a_{pos+1}
an∗(n−pos)+an−1∗(n−pos−1)+⋯+apos+1,once=
a
n
+
a
n
−
1
+
⋯
+
a
p
o
s
+
1
a_n+a_{n-1}+\cdots +a_{pos+1}
an+an−1+⋯+apos+1,然后直接套用数位dp模版代码即可。
下面是AC代码。
#include<iostream>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<sstream>
#include<cstring>
#include<stack>
#include<functional>
#include<unordered_map>
using namespace std;
using ld=long double;
struct fastio{fastio(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}}fio;
#define int long long
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<":"<<x<<" "<<#y<<":"<<y<<endl
#define debug3(x,y,z) cerr<<#x<<":"<<x<<" "<<#y<<":"<<y<<" "<<#z<<":"<<z<<endl
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define rev(x) (x).rbegin(),(x).rend()
#define cr(x) const x&
int dp[20][2000][200];
vector<int> num;
int dfs(int pos,int pre,int once,bool limit){
if(pos>=num.size()){
return !once||pre%once==0;
}
if((dp[pos][pre][once]!=-1&&(!limit))) return dp[pos][pre][once];
int res=0;
int bound=limit?num[pos]:9;
for(int i=0;i<=bound;i++){
int t=once+i;
res+=dfs(pos+1,pre+t,t,i==bound&&limit);
}
if(!limit) dp[pos][pre][once]=res;
return res;
}
int solve(int x){
num.clear();
while(x){
num.push_back(x%10);
x/=10;
}
memset(dp,-1,sizeof(dp));
num=vector<int>(rev(num));
return dfs(0,0,0,true);
}
signed main(){
int T,l,r;
cin>>T;
while(T--){
cin>>l>>r;
if(l) cout<<solve(r)-solve(l-1)<<endl;
else cout<<solve(r)<<endl;
}
}