天天看點

HDU3709 Balanced Number 數位DP+記憶化DFS

                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 ≤ 10 18).  

Output For each case, print the number of balanced numbers in the range [x, y] in a line.  

Sample Input

20 97604 24324  

Sample Output

10897  

題意:找出區間内平衡數的個數,所謂的平衡數,就是以這個數字的某一位為支點,另外兩邊的數字大小乘以力矩之和相等,即為平衡數

思路:按位枚舉,找出所有可能的狀況進行dfs,與POJ3252類似

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int bit[];__int64 dp[][][];//pos為目前位置//o為支點//l為力矩//work為是否有上限__int64 dfs(int pos,int o,int l,int work){    if(pos == )        return l == ;//已經全部組合完了    if(l<)//力矩和為負,則後面的必然小于0        return ;    if(!work && dp[pos][o][l]!=)//沒有上限,且已經被搜尋過了        return dp[pos][o][l];    __int64 ans = ;    int end = work?bit[pos]:;//有上限就設為上限,否則就設為9    for(int i=; i<=end; i++)    {        int next = l;        next += (pos-o)*i;//力矩        ans+=dfs(pos,o,next,work&&i==end);    }    if(!work)        dp[pos][o][l] = ans;    return ans;}__int64 solve(__int64 n){    int len = ;    while(n)    {        bit[len++] = n%;        n/=;    }    __int64 ans = ;    for(int i = ; i<len; i++)    {        ans+=dfs(len,i,,);    }    return ans-(len);//排除掉0,00,000....這些情況}int main(){    int T;    __int64 l,r;    scanf("%d",&T);    memset(dp,,sizeof(dp));    while(T--)    {        scanf("%I64d%I64d",&l,&r);        printf("%I64d\n",solve(r)-solve(l));    }    return ;}
           

                            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 ≤ 10 18).  

Output For each case, print the number of balanced numbers in the range [x, y] in a line.  

Sample Input

20 97604 24324  

Sample Output

10897  

題意:找出區間内平衡數的個數,所謂的平衡數,就是以這個數字的某一位為支點,另外兩邊的數字大小乘以力矩之和相等,即為平衡數

思路:按位枚舉,找出所有可能的狀況進行dfs,與POJ3252類似

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int bit[];__int64 dp[][][];//pos為目前位置//o為支點//l為力矩//work為是否有上限__int64 dfs(int pos,int o,int l,int work){    if(pos == )        return l == ;//已經全部組合完了    if(l<)//力矩和為負,則後面的必然小于0        return ;    if(!work && dp[pos][o][l]!=)//沒有上限,且已經被搜尋過了        return dp[pos][o][l];    __int64 ans = ;    int end = work?bit[pos]:;//有上限就設為上限,否則就設為9    for(int i=; i<=end; i++)    {        int next = l;        next += (pos-o)*i;//力矩        ans+=dfs(pos,o,next,work&&i==end);    }    if(!work)        dp[pos][o][l] = ans;    return ans;}__int64 solve(__int64 n){    int len = ;    while(n)    {        bit[len++] = n%;        n/=;    }    __int64 ans = ;    for(int i = ; i<len; i++)    {        ans+=dfs(len,i,,);    }    return ans-(len);//排除掉0,00,000....這些情況}int main(){    int T;    __int64 l,r;    scanf("%d",&T);    memset(dp,,sizeof(dp));    while(T--)    {        scanf("%I64d%I64d",&l,&r);        printf("%I64d\n",solve(r)-solve(l));    }    return ;}