• HDU 3709


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709

    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

    题意:

    求区间[L,R]内平衡数的个数。

    平衡数:选一个点作为支点,左右侧力矩相等即为平衡,例如4139把支点选在3,左侧sum = 4*2+1*1 = 9,右侧sum = 9*1 = 9;

    题解:

    参考http://blog.csdn.net/dgq8211/article/details/9302069

    首先要分析出,对于某个非 0 的 number,最多可能有一个 pivot 的位置。
    证明:如果有两个这样的位置,将左边位置移动到右边时,左边的 sigma 一定增大,右边的 sigma 最多保证不减,不可能增大,故不可能再次相等。
    于是可以枚举这样的位置,然后分类统计求和。
    由于数字0对于每个pivot=1~len的位置都会被统计到,最后要减去重复的。
    

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int dig[20];
    ll dp[20][20][1500];
    
    ll dfs(int pos,int piv,int sum,bool limit)
    {
        if(sum<0) return 0; //右侧力矩已经大于左侧力矩,已经不可能平衡,不需要再往低位DFS,直接剪枝
        if(pos==0) return sum==0; //已经精确到某一个数,观察一下两侧力矩是否相等.
        if(!limit && dp[pos][piv][sum]!=-1) return dp[pos][piv][sum]; //如果曾计算过dfs(pos,piv,sum,0),直接返回dp[pos][piv][sum]
    
        int up=limit?dig[pos]:9; //确定当前第pos位的枚举上界
        ll ans=0; //定义ans变量,记录累加结果,在函数最后return ans
        for(int i=0;i<=up;i++) //暴力枚举当前第pos位的数
            ans+=dfs(pos-1,piv,sum+i*(pos-piv),limit && i==up);
    
        if(!limit) dp[pos][piv][sum]=ans; //将dfs(pos,piv,sum,0)记录到dp[pos][piv][sum]
        return ans;
    }
    ll solve(ll x)
    {
        int len=0;
        while(x) //将上界N记录到dig数组中
        {
            dig[++len]=x%10;
            x/=10;
        }
        ll ans=0;
        for(int i=1;i<=len;i++) ans+=dfs(len,i,0,1);
        return ans-len+1;
    }
    
    int main()
    {
        int t;
        ll l,r;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%I64d%I64d",&l,&r);
            memset(dp,-1,sizeof(dp));
            printf("%I64d
    ",solve(r)-solve(l-1));
        }
    }

    注意点:

  • 相关阅读:
    游记 Day10
    游记 Day9
    NOIP模拟测试10
    【贪心】P3942 将军令 && P2279 消防局的设立
    在没有上考场之前,菜鸡也有翻盘的机会
    【数据结构】 圆方树&&广义圆方树
    快速幂&&龟速乘&&快速乘
    游记 Day 4
    【容斥】[ZJOI2016] 小星星
    游记 Day3
  • 原文地址:https://www.cnblogs.com/dilthey/p/8510500.html
Copyright © 2020-2023  润新知