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 std;
typedef long long ll;
ll x , y , dp[20][20][2000];
int dig[20];
ll dfs(int len , int mid , int cau , int flag) {
    if(!len)
        return cau == 0;
    if(cau < 0)
        return 0;
    if(!flag && dp[len][mid][cau] != -1)
        return dp[len][mid][cau];
    int t = flag ? dig[len] : 9;
    ll sum = 0;
    for(int i = 0 ; i <= t ; i++) {
        sum += dfs(len - 1 , mid , cau + i * (len - mid) , flag && i == t);
    }
    if(!flag)
        dp[len][mid][cau] = sum;
    return sum;
}
ll Gets(ll gg) {
    memset(dp , -1 , sizeof(dp));
    int len = 0;
    if(gg < 0)
        return 0;
    if(gg == 0)
        return 1;
    while(gg) {
        dig[++len] = gg % 10;
        gg /= 10;
    }
    ll ans = 0;
    for(int i = len ; i >= 1 ; i--) {
        ans += dfs(len , i , 0 , 1);
    }
    return ans - (len - 1);
}
int main() {
    int t;
    scanf("%d" , &t);
    while(t--) {
        scanf("%lld%lld" , &x , &y);
        printf("%lld\n" , Gets(y) - Gets(x - 1));
    }
    return 0;
}
时间: 2024-10-20 15:11:00

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 - 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

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