zoj 3416 统计平衡数个数

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].

思路:

枚举中心轴的位置,然后针对这个位置,算出结果,结果记得去掉全0的情况。

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using std::swap;
 6 using std::cin;
 7 using std::cout;
 8 using std::endl;
 9 typedef long long LL;
10 int const N = 25;
11 int const M = 200;
12 LL dp[N][N][M];
13 int bit[N],ll;
14 LL getsum1(int t,int pre,int center,int limit)
15 {
16    if(!t)return pre==0;
17    if(pre<0)return 0;
18    if(!limit&&dp[t][center][pre]!=-1)return dp[t][center][pre];
19    int up=(limit?bit[t]:9);
20    LL ans=0;
21    for(int i=0;i<=up;i++)
22    {
23        ans+=getsum1(t-1,pre+(t-center)*i,center,limit&&(i==up));
24    }
25    if(!limit&&dp[t][center][pre]==-1)dp[t][center][pre]=ans;
26    return ans;
27 }
28 LL getsum2(LL n)
29 {
30    if(n<0)return 0;
31    for(ll=0;n;n/=10)bit[++ll]=n%10;
32    LL ans=0;
33    for(int i=1;i<=ll;i++)
34        ans+=getsum1(ll,0,i,1);
35    return ans-ll+1;
36 }
37 int main()
38 {
39     int T;
40     memset(dp,-1,sizeof(dp));
41     scanf("%d",&T);
42     LL l,r;
43     while(T--)
44     {
45           scanf("%lld %lld",&l,&r);
46           printf("%lld\n",getsum2(r)-getsum2(l-1));
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/nuoyan2010/p/3065935.html