HDU - 3709 - Balanced Number(数位DP)

链接:

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\n", Solve(b)-Solve(a-1));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12000186.html

时间: 2024-12-06 18:49:56

HDU - 3709 - Balanced Number(数位DP)的相关文章

HDU 3709 Balanced Number (数位DP)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3798    Accepted Submission(s): 1772 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

hdu 3709 Balanced Number (数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1871    Accepted Submission(s): 836 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

HDU 3709 Balanced Number 枚举+数位DP

枚举支点之后数位DP,注意姿势 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list&g

hdu 3709 Balanced Number(平衡数)--数位dp

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 9036    Accepted Submission(s): 4294 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

HDU 3709 Balanced Number

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3988    Accepted Submission(s): 1869 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

hdu 5898 odd-even number(数位dp)

Problem Description For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<= 9*10^18). Input Fir

hdu 3709 Balanced Number(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3709 题意:给定区间[a,b],求区间内平衡数的个数.所谓平衡数即有一位做平衡点,左右两边数字的力矩相等. 求力矩很显然可以想到dp[len][mid][cau],mid表示对称点,cau表示力矩大小. 然后很显然的记忆化索索 #include <iostream> #include <cstring> #include <cstdio> using namespace s