数位dp HDU 3709 Balanced Number

Balanced Number

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


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
 


 

Source

 

 


 

 

题意:要你找出[x,y]的平衡数的数目。

 

思路:我们用 dp[i][j][k]表示i位数,对称中心为j,平衡情况为k的数有多少个,那么我们能得出递推式dp[i+1][j][k+(j-(i+1)*val]+=dp[i][j][k] 这个我用bfs来推的。因为k并不一定不是每一个都有,所以用for循环可能会有很多无用的状态。然后输出的话,套个dfs的模板就行了,最后要注意的是0被我们算了多次,要减去重复的。

 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=2*2000+5;
const int zero=2000;
#define LL long long
LL dp[20][20][maxn];
char digit[20];
int n;

struct State
{
    int ps,pv,bal;
    State(int ps,int pv,int bal)
    :ps(ps),pv(pv),bal(bal) {}
};

bool vis[20][20][maxn];
void pre_init()
{
    memset(dp,0,sizeof(dp));
    queue<State> q;
    for(int p=0;p<=19;p++) {
        dp[0][p][zero]=1;
        q.push(State(0,p,zero));
    }
    memset(vis,0,sizeof(vis));
    while(q.size()) {
        State tmp=q.front(); q.pop();
        int i=tmp.ps,j=tmp.pv,k=tmp.bal;
     //   printf("%d %d %d: %lld\n",i,j,k,dp[i][j][k]);
        if(i==19) continue;
        for(int val=0;val<=9;++val) {
            int kk=k+(j-(i+1))*val;
            dp[i+1][j][kk]+=dp[i][j][k];
            if(!vis[i+1][j][kk]) q.push(State(i+1,j,kk));
            vis[i+1][j][kk]=true;
        }
    }
}

LL dfs(int cur,int pivot,int bal,bool limit)
{
    if(!limit || cur==0) return dp[cur][pivot][2*zero-bal];
    int tail=limit?digit[cur]-'0':9;
    LL ret=0;
    for(int val=0;val<=tail;++val)
        ret+=dfs(cur-1,pivot,bal+(pivot-cur)*val,limit&&val==tail);
    return ret;
}

LL Cal_Num()
{
    LL ret=0;
    for(int i=1;i<=n;++i)
        ret+=dfs(n,i,zero,true);
    return ret-(n-1);
}

void Test()
{
    int i,j,k;
    while(cin>>i>>j>>k) {
        k+=zero;
        cout<<dp[i][j][k]<<endl;
    }
}

int bal_cnt[100000];

bool balance(int p,char * s,int n)
{
    int val=0;
    for(int i=1;i<=n;++i)
        val+=(p-i)*(s[i]-'0');
    return val==0;
}

bool is_bal(int x)
{
    char s[1000]; sprintf(s+1,"%d",x);
    n=strlen(s+1);
    for(int pivot=1;pivot<=n;++pivot)
        if(balance(pivot,s,n)) return true;
    return false;
}

void Brute()
{
    bal_cnt[0]=1;
    for(int i=1;i<100000;++i)
    {
        bal_cnt[i]=bal_cnt[i-1]+(is_bal(i));
       // printf("%d: %d\n",i,bal_cnt[i]);
    }
    int x;
    freopen("out.txt","w",stdout);
    for(int i=0;i<100000;++i) printf("%d: %d\n",i,bal_cnt[i]);
}

void GetData()
{
    freopen("in.txt","w",stdout);
    printf("1000000\n");
    for(int i=0;i<1000000;++i) printf("0 %d\n",i);
}

int main()
{
   // GetData(); return 0;
//   freopen("in.txt","r",stdin);
//   freopen("out.txt","w",stdout);
  //  Brute(); return 0;
    pre_init();
   // Test();
    int T;cin>>T;
    int cnt=0;
    while(T--)
    {
        LL x; scanf("%I64d",&x);
        LL ans=0;
        if(x>0) {
            --x;
            sprintf(digit+1,"%I64d",x);
            n=strlen(digit+1);
            reverse(digit+1,digit+1+n);
            ans=Cal_Num();
        }
        scanf("%s",digit+1);
        n=strlen(digit+1);
        reverse(digit+1,digit+1+n);
        ans = Cal_Num()-ans;
        printf("%I64d\n",ans);
    }
}




 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值