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
2 0 9 7604 24324
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