码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3709 数位dp

时间:2017-03-24 22:11:59      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:ring   math   div   stream   bug   ref   mit   scan   acm   

Balanced Number

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


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
 

 

Author
GAO, Yuan
 

 

Source
 题意:给你一个数number,你可以确定某位,然后如果分成的左右两部分的 sigma(d[i] * | i - fixloc |)相等,则它是 Balanced Number。统计区间 [a,b] 中Balance Number 的个数。
 题解:枚举分界点 套路数位dp 注意0 在 数位dp 的 dfs 中,对于 0 的看待,是 len 个0 放一起
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define dazhi 2147483647
12 #define bug() printf("!!!!!!!")
13 #define M 100005
14 using namespace  std;
15 ll a,b;
16 ll f[30][30][2005];
17 int bit[30];
18 ll dp(int pos,int x,int st,int flag)
19 {
20    if(pos==0)  return st==0;
21    if(st<0) return 0;
22    if(flag&&f[pos][x][st]!=-1) return f[pos][x][st];
23    int d=flag?9:bit[pos];
24    ll re=0;
25    for(int i=0;i<=d;i++)
26    {
27        re+=dp(pos-1,x,st+i*(pos-x),flag||i!=d);
28    }
29    if(flag) f[pos][x][st]=re;
30    return re;
31 }
32 ll fun(ll n)
33 {
34     if(n<0) return 0;
35     int len=0;
36     while(n>0)
37     {
38         bit[++len]=n%10;
39         n/=10;
40     }
41     ll ans=0;
42     for(int i=1;i<=len;i++)
43     {
44         ans+=dp(len,i,0,0);
45     }
46     return ans-len+1;
47 }
48 int main()
49 {
50     int t;
51     scanf("%d",&t);
52     memset(f,-1,sizeof(f));
53     while(t--)
54     {
55         scanf("%I64d %I64d",&a,&b);
56         printf("%I64d\n",fun(b)-fun(a-1));
57     }
58     return 0;
59 }

 

HDU 3709 数位dp

标签:ring   math   div   stream   bug   ref   mit   scan   acm   

原文地址:http://www.cnblogs.com/hsd-/p/6613413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!