zoukankan      html  css  js  c++  java
  • hdu 3709 Balanced Number 数位dp

    Balanced Number

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)


    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
     

     思路:加一维枚举中间点即可;

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-4
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=1e6+10,inf=2147483647;
    const ll INF=1e18+10,mod=2147493647;
    ll f[30][30][2000],bit[60];
    ll dp(int pos,int sum,int p,int flag)
    {
        if(sum<0)return 0;
        if(pos==0)return (sum==0);
        if(flag&&f[pos][p][sum]!=-1)return f[pos][p][sum];
        int x=flag?9:bit[pos];
        ll ans=0;
        for(int i=0;i<=x;i++)
        {
            ans+=dp(pos-1,sum+(pos-p)*i,p,flag||i<x);
        }
        if(flag)f[pos][p][sum]=ans;
        return ans;
    }
    ll getans(ll x,int p)
    {
        int len=0;
        while(x)
        {
            bit[++len]=x%10;
            x/=10;
        }
        return dp(len,0,p,0);
    }
    int main()
    {
        int T;
        memset(f,-1,sizeof(f));
        scanf("%d",&T);
        while(T--)
        {
            ll l,r;
            scanf("%lld%lld",&l,&r);
            ll ans=0;
            if(!l)
            {
                ans++;
                l=max(1LL,l);
            }
            for(int i=1;i<=19;i++)
                ans+=getans(r,i)-getans(l-1,i);
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    hdu 5646 DZY Loves Partition
    bzoj 1001 狼抓兔子 平面图最小割
    poj 1815 Friendship 最小割 拆点 输出字典序
    spoj 1693 Coconuts 最小割 二者取其一式
    hdu 5643 King's Game 约瑟夫环变形
    约瑟夫环问题
    hdu 5642 King's Order
    CodeForces 631C Report
    1039: C语言程序设计教程(第三版)课后习题9.4
    1043: C语言程序设计教程(第三版)课后习题10.1
  • 原文地址:https://www.cnblogs.com/jhz033/p/6595249.html
Copyright © 2011-2022 走看看