Balanced Number(数位DP)

Balanced Number

http://acm.hdu.edu.cn/showproblem.php?pid=3709

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 8653    Accepted Submission(s): 4127


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 ≤ 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
 
Author
GAO, Yuan
 

枚举每一位数当成中点,然后数位DP。注意最后要减去0的情况,eg:0、00、000、0000....

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 13000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long MOD=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 ll dp[25][25][5005];
24 int a[25];
25 
26 ll dfs(int pos,int x,int sum,bool limit){
27     if(pos==-1) return !sum;
28     if(!limit&&dp[pos][x][sum]!=-1) return dp[pos][x][sum];
29     int up=limit?a[pos]:9;
30     ll ans=0;
31     for(int i=0;i<=up;i++){
32         ans+=dfs(pos-1,x,sum+i*(pos-x),limit&&i==a[pos]);
33     }
34     if(!limit) dp[pos][x][sum]=ans;
35     return ans;
36 }
37 
38 ll solve(ll x){
39     int pos=0;
40     while(x){
41         a[pos++]=x%10;
42         x/=10;
43     }
44     ll ans=0;
45     for(int i=0;i<pos;i++){
46         ans+=dfs(pos-1,i,0,true);
47     }
48     return ans-pos+1;
49 }
50 
51 int main(){
52     #ifndef ONLINE_JUDGE
53      //   freopen("1.txt","r",stdin);
54     #endif
55     std::ios::sync_with_stdio(false);
56     int t;
57     cin>>t;
58     ll n,m;
59     memset(dp,-1,sizeof(dp));
60     while(t--){
61         cin>>n>>m;
62         ll ans=solve(m)-solve(n-1);
63         cout<<ans<<endl;
64     }
65 }
View Code

猜你喜欢

转载自www.cnblogs.com/Fighting-sh/p/10523621.html