hdu3709——Balanced Number(数位dp)
2016-09-23 18:13
429 查看
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位时的值
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 <iostream> #include <cstring> #include <string> #include <vector> #include <queue> #include <cstdio> #include <set> #include <cmath> #include <map> #include <algorithm> #define INF 0x3f3f3f3f #define MAXN 10000000005 #define Mod 10001 using 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<cnt;++i) ans+=dfs(cnt-1,i,1,0); cnt--; return ans-cnt+1; } int main() { memset(dp,-1,sizeof(dp)); int t; scanf("%d",&t); while(t--) { long long x,y; scanf("%I64d%I64d",&x,&y); printf("%I64d\n",solve(y)-solve(x-1)); } return 0; }
相关文章推荐
- HDU 3709 Balanced Number ZOJ 3416 Balanced Number(数位DP)
- hdu 3709 Balanced Number(数位DP,5级)
- HDU 3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number (简单数位DP)
- HDU 3709 Balanced Number(数位DP)
- HDU 3709 Balanced Number 枚举+数位DP
- HDU - 3709 Balanced Number (数位DP&记忆化dfs)好题
- hdu 3709 Balanced Number 数位dp
- HDU-3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number (数位DP)
- HDU_3709 Balanced Number 数位dp
- hdu 3709 Balanced Number 数位DP
- HDU 3709 Balanced Number 数位dp
- HDU 3709 Balanced Number (数位dp)
- hdu 3709 Balanced Number(数位DP,5级)
- HDU 3709: Balanced Number (数位DP)
- HDU 3709 Balanced Number 数位DP
- 【数位DP】 HDU 3709 Balanced Number
- HDU 3709 Balanced Number(数位DP)
- 【HDU】3709 Balanced Number 数位DP