程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> hdu 3709 數位dp(小思維)

hdu 3709 數位dp(小思維)

編輯:關於C++

 

 

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
/**
hdu 3709   數位dp(小思維)
解題思路:有一個很好的轉化技巧,不然會超時。搜索的時候初始值定為f(x),然後最後和0比較。不要搜f(i) 和f(x)比較
*/
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL ;
LL dp[25][25][2000],l,r;
int bit[25];
LL dfs(int len,int pos,int sum,int flag)
{
    if(len<0)
    {
        //printf(%d %d>>>>
,suml,sumr);
        return sum==0;
    }
    if(flag==0&&dp[len][pos][sum]!=-1)
        return dp[len][pos][sum];
    int end=flag?bit[len]:9;
    LL ans=0;
    for(int i=0;i<=end;i++)
    {
        //printf(len-1:%d
,len-1);
        ans+=dfs(len-1,pos,(sum+(len-pos)*i),flag&&i==end);
    }
    if(flag==0)
    {
        dp[len][pos][sum]=ans;
    }
    return ans;
}
LL solve(LL n)
{
    if(n==-1)return 0;
    int len=0;
    while(n)
    {
        bit[len++]=n%10;
        n/=10;
    }
    LL ans=0;
    for(int i=0;i

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved