zoukankan      html  css  js  c++  java
  • HDU

    链接:

    https://vjudge.net/problem/HDU-3709

    题意:

    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 42 + 11 = 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].

    思路:

    刚开始没算具体大小。。以为直接算会暴空间。
    记录当前的力矩和,让高位表示正数,这样在从高位到低位的时候,如果力矩边成负值,可以直接返回0,因为低位的权值是负的。
    同时要枚举每一个位置作为支点的情况,再去减掉每次算的0.

    代码:

    // #include<bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string.h>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    typedef long long LL;
    const int MOD = 1e9+7;
    const int MAXN = 1e6+10;
    
    LL F[20][20][1600];
    LL dig[20];
    LL a, b;
    
    LL Dfs(int pos, int piv, int sum, bool lim)
    {
        if (pos == -1)
            return sum == 0;
        if (sum < 0)
            return 0;
        if (!lim && F[pos][piv][sum] != -1)
            return F[pos][piv][sum];
        int up = lim ? dig[pos] : 9;
        LL ans = 0;
        for (int i = 0;i <= up;i++)
            ans += Dfs(pos-1, piv, (pos-piv)*i+sum, lim && i == up);
        if (!lim)
            F[pos][piv][sum] = ans;
        return ans;
    }
    
    LL Solve(LL x)
    {
        int p = 0;
        while(x)
        {
            dig[p++] = x%10;
            x /= 10;
        }
        LL ans = 0;
        for (int i = 0;i < p;i++)
            ans += Dfs(p-1, i, 0, true);
        return ans-(p-1);
    }
    
    int main()
    {
        // freopen("test.in", "r", stdin);
        memset(F, -1, sizeof(F));
        int t;
        scanf("%d", &t);
        while(t--)
        {
            scanf("%lld%lld", &a, &b);
            printf("%lld
    ", Solve(b)-Solve(a-1));
        }
    
        return 0;
    }
    
  • 相关阅读:
    linux-centos7-vmware 虚拟机 设置静态 ip 地址,采用 nat 模式,实现连接公网
    mysql_5.7.20 二进制包 在Linux系统中的 安装和配置
    解决windows系统80端口被占用问题
    应用系统架构演变初探
    fastdfs-nginx扩展模块源码分析
    心已落定,入驻博客园
    mac iterm 提示符序列调整
    Background removal with deep learning
    git push fatal: The remote end hung up unexpectedly
    mac sed 使用踩坑实录
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12000186.html
Copyright © 2011-2022 走看看