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;
    }
  • 相关阅读:
    忘记oracle的sys用户密码怎么修改
    C语言 给字符数组赋值的方法
    linux编译中的常见问题
    Ubuntu下查看linux版本,内核版本,系统位数,gcc版本
    Win7中打开chm文件内容无法显示问题
    exit()与_exit()函数的区别(Linux系统中)
    【BZOJ3456】城市规划(生成函数,多项式运算)
    【BZOJ3028】食物(生成函数)
    【CF438E】The Child and Binary Tree(多项式运算,生成函数)
    【BZOJ3771】Triple(生成函数,多项式运算)
  • 原文地址:https://www.cnblogs.com/jhz033/p/6595249.html
Copyright © 2011-2022 走看看