https://cn.vjudge.net/contest/312854#problem/D
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 ≤ 10 18).
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
题意:对于某个 数,你可以定义 fix a pivot 在某位,然后如果分成的左右两部分的 sigma(d[i] * | i - fixloc |)相等,则它是 Balanced Number。
统计区间 [a,b] 中Balance Number 的个数。
嗯,因为位数不大,我们枚举这个fixloc的位置,那么我们每次搜索的时候就只需要判定最后sum是不是等于0即可。
dp[i][j][k]分别表示长度为i,fixloc的位置为j,和为k的这个平衡数的个数。
看代码应该就懂了。对于这个题意的理解很重要,这样翻译过来就很容易操作了,我们不是去判定左右两部分相等,而是去判定,最后的和是不是等于0.这样就好操作多了。感觉数位dp非常套路就是记忆化搜索。
注意:0的时候比较特殊,枚举哪个位置它都符合条件。所以最后要减去重复的。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
ll dp[20][20][1500],a[20];
//pos--长度,balance--平衡点位置,sum--从高位向低位累加(权值*(balance-pos))
ll dfs(ll pos,ll balance,ll sum,ll limit){
if(pos<=0)return sum==0;//如果sum==0说明是平衡数
if(!limit&&dp[pos][balance][sum]!=-1)return dp[pos][balance][sum];
ll up=limit?a[pos]:9;
ll ans=0;
for(ll i=0;i<=up;i++){
ans+=dfs(pos-1,balance,sum+(balance-pos)*i,limit&&i==a[pos]);
}
if(!limit){
dp[pos][balance][sum]=ans;
}
return ans;
}
ll getsum(ll n){
ll len=0;
while(n){
a[++len]=n%10;
n/=10;
}
ll ans=0;
//枚举所有的平衡点
for(ll i=1;i<=len;i++){
ans+=dfs(len,i,0,1);
}
//枚举所有的平衡点时0都算进去了
//多算了(len-1)个0
return ans-len+1;
}
int main(){
ll t;
cin>>t;
memset(dp,-1,sizeof(dp));
while(t--){
ll n,m;
cin>>n>>m;
cout<<getsum(m)-getsum(n-1)<<endl;
}
return 0;
}
// /\ | / |**、
// / \ | / | \
// / \ |/ | / _____ ____ | /
// /------\ |\ |__/ / \ \ /\ / / \ | /
// / \ | \ | / \ \ / \ / /______\ |/
// / \ | \ | \ / \ / \ / \ |
// / \ | \ | \_____/ \/ \/ \_____ |
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//