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;
    }
  • 相关阅读:
    AS3中的xml
    HTML5 tools, Animation tools Adobe Edge Preview
    重新审视php+mssql
    SVN合并分支,从主干合并到分支
    AIR HTML相关资料[js部分]
    USACO 1.1friday
    H.High String
    POJ 3670 Eating Together
    HDU 1203:I NEED A OFFER!
    CodeForces #1 B. Spreadsheets
  • 原文地址:https://www.cnblogs.com/jhz033/p/6595249.html
Copyright © 2011-2022 走看看