zoukankan      html  css  js  c++  java
  • Balanced Number

    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
    分析:注意观察可以发现每个数至多有一个支撑点;
         这样枚举支撑点即可;
       三维dp[i][j][k],分别代表位置,支撑点,当前力矩;
       注意0多算了pos-1次;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    const int N=5e4+10;
    const int M=N*10*10;
    using namespace std;
    inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline void umax(ll &p,ll q){if(p<q)p=q;}
    inline void umin(ll &p,ll q){if(p>q)p=q;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,num[20],pos;
    ll dp[20][20][2000],p,q;
    ll dfs(int pos,int x,int y,int z)
    {
        if(pos<0)return y==0;
        if(y<0)return 0;
        if(z&&dp[pos][x][y]!=-1)return dp[pos][x][y];
        int now=z?9:num[pos],i;
        ll ret=0;
        rep(i,0,now)
        {
            ret+=dfs(pos-1,x,y+i*(pos-x),z||i<num[pos]);
        }
        return z?dp[pos][x][y]=ret:ret;
    }
    ll gao(ll p)
    {
        if(p<0)return 0;
        pos=0;
        while(p)num[pos++]=p%10,p/=10;
        ll ret=0;
        int i;
        rep(i,0,pos-1)ret+=dfs(pos-1,i,0,0);
        return ret-pos+1;
    }
    int main()
    {
        int i,j;
        memset(dp,-1,sizeof(dp));
        scanf("%d",&t);
        while(t--)
        {
            scanf("%lld%lld",&p,&q);
            printf("%lld
    ",gao(q)-gao(p-1));
        }
        return 0;
    }
  • 相关阅读:
    phone8 inapp purchasing
    Delegate,Action,Func,匿名方法,匿名委托,事件
    get a full screen popup to work on WP8
    as3.0动态文本大全
    actionScript 3 殿堂之路 第四章 学习收获
    java中的switch case
    Silverlight的DataGrid导出文档
    ArcGIS教程大全
    同步异步请求区别
    XML操作,XPath语法
  • 原文地址:https://www.cnblogs.com/dyzll/p/6390817.html
Copyright © 2011-2022 走看看