题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709
题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <vector> 7 #include <ctime> 8 #include <queue> 9 #include <list> 10 #include <set> 11 #include <map> 12 using namespace std; 13 #define INF 0x3f3f3f3f 14 typedef long long LL; 15 int bit[20]; 16 LL dp[20][20][2000]; 17 18 LL solve(int len, int mid, int blan, int flag) 19 { 20 LL sum = 0; 21 if(len == 0) 22 return blan == 0; 23 if(blan < 0) 24 return 0; 25 if(flag && dp[len][mid][blan] >= 0) 26 return dp[len][mid][blan]; 27 int te = flag ? 9 : bit[len]; 28 for(int i = 0; i <= te; i++) 29 { 30 int Blan = blan + (len - mid) * i; 31 sum += solve(len - 1, mid, Blan, flag || i < te ); 32 } 33 if(flag) 34 dp[len][mid][blan] = sum; 35 return sum; 36 } 37 int main() 38 { 39 int T; 40 LL x, y; 41 scanf("%d", &T); 42 while(T--) 43 { 44 scanf("%I64d %I64d", &x, &y); 45 if(x == 0 && y == 0) 46 { 47 printf("1 "); 48 continue; 49 } 50 x--; 51 memset(bit, 0, sizeof(bit)); 52 memset(dp, -1, sizeof(dp)); 53 int len = 0; 54 while(x) 55 { 56 bit[++len] = x % 10; 57 x /= 10; 58 } 59 LL res = 1-len;//排除掉00,000,0000....这些情况 60 for(int i = 1; i <= len; i++) 61 res += solve(len, i, 0, 0); 62 memset(bit, 0, sizeof(bit)); 63 memset(dp, -1, sizeof(dp)); 64 len = 0; 65 while(y) 66 { 67 bit[++len] = y % 10; 68 y /= 10; 69 } 70 LL res2 = 1-len; 71 for(int i = 1; i <= len; i++) 72 res2 += solve(len, i, 0, 0); 73 res = res2 - res; 74 printf("%I64d ", res); 75 } 76 return 0; 77 }