hdu 3709Balanced Number(数位dp)

Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 2640    Accepted Submission(s): 1196

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

Author

GAO, Yuan

Source

2010 Asia Chengdu Regional Contest

Recommend

zhengfeng

题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩                之和相等,即为平衡数

题解:枚举支点。一开始以为同一个数以不同的数位为支点会可能都是平衡数,这样就会算重复。然后仔细想想不可能

如果一开始以第一个为支点,左边为suml,支点右边为sumr,然后支点左移,suml是递减的,sumr是递增的。所以

不会重复。(0,00,000除外)

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#define ll long long
#define N 1505

using namespace std;

ll l,r;
ll dp[20][20][N];///dp[i][j][k] i:长度 j:支点 k:和
int num[30];

ll dfs(int i,int z,int sum,bool e) {
    if(i<=0)return sum==0;
    if(sum<0)return 0;
    if(!e&&dp[i][z][sum]!=-1)return dp[i][z][sum];
    ll res=0;
    int u=e?num[i]:9;
    for(int d=0; d<=u; d++) {
        int s=sum+(i-z)*d;
        res+=dfs(i-1,z,s,e&&d==u);
    }
    return e?res:dp[i][z][sum]=res;
}

ll solve(ll n) {
    int len=1;
    while(n) {
        num[len++]=n%10;
        n/=10;
    }
    ll ans=0;
    for(int i=1; i<=len-1; i++) {///枚举支点
        ans+=dfs(len-1,i,0,1);
    }
    return ans-len+1;
}

int main() {
    memset(dp,-1,sizeof dp);
    int t;
    cin>>t;
    while(t--) {
        scanf("%lld%lld",&l,&r);
        printf("%lld\n",solve(r)-solve(l-1));
    }
    return 0;
}
时间: 2024-10-15 14:02:58

hdu 3709Balanced Number(数位dp)的相关文章

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

Hdu3079Balanced Number数位dp

枚举支点,然后就搞,记录之前的点的力矩和. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <s

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

多校5 HDU5787 K-wolf Number 数位DP

1 // 多校5 HDU5787 K-wolf Number 数位DP 2 // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d 3 // f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小 4 // 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位 5 // http://blog.csdn.net/weizhuwyzc000/article/details/5209769

HDU——B-number(数位DP)

题目大意: 要找出1到n之间有多少个数含13,并且能被13整除 记忆化搜索: dp[pos][pre][mod][statu],pos位数,pre前一位,mod余数,statu状态 有2个状态:含13,不含13 <span style="font-size:18px;"><strong>#include<iostream> #include<cstring> #include<cstdio> #include<algor

hdu 5787 K-wolf Number 数位dp

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

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