hdu3709 (balanced number) digital DP

Source: Internet
Author: User

Balanced number

Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 2945 Accepted Submission (s): 1348


Problem Descriptiona Balanced number is a non-negative integer the can be balanced if a pivot was placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot was placed at some digit of the number, the distance from a digit to the pivot is the offset between it and th e pivot. Then the torques of left part and right part can calculated. It's balanced if they is the same. A balanced number must is balanced with the pivot at some of its digits. For example, 4139 are a balanced number with pivot fixed at 3. The torqueses is 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].

Inputthe input contains multiple test cases. The first line was the total number of cases T (0 < t≤30). For each case, there is integers separated by a space in a line, X and Y. (0≤x≤y≤1018).

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

Sample Input20 97604 24324

Sample Output10897

Authorgao, Yuan

Source of Asia Chengdu regional Contest

Recommendzhengfeng | We have a carefully selected several similar problems for you:3711 3715 3718 3713 3712 Enumerate the center points, and then the bitwise enumeration, length from Len to 0, calculates the sum of the current weights and the pre, and Determines whether the pre value is 0 at the end of the post, 0, returns 1, or returns 1. Use Dp[len][cen][pre] to save the length of I, the center point is k, the sum of the preceding weights is the number of the pre of the state of how many. Finally need to remove all 0 of the situation, because all is 0, the center point can be arbitrarily enumerated, so need to subtract (len-1).
#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>using namespacestd;#defineLL Long Long#defineMAXN 30LL dp[maxn][maxn][2500];//Dp[i][k][j] Represents the length of I, the center point is K, the highest bit to the position with the highest distance of I the number of weights of the sum of the number of J and how many kindsLL Digit[maxn];__int64 DFS (intLenintCenintPreBOOLfp//The DFS version is purely violent enumeration of each number, while the recursive version is considered the effect of the prefix{    if(len==0)        returnpre==0; if(pre<0)         return 0; if(!FP && dp[len][cen][pre]! =-1)//    {         returnDp[len][cen][pre]; } LL ret=0; intFpmax = fp? Digit[len]:9;  for(intI=0; i<=fpmax;i++)//figure out the number of schemes that begin with I, respectively,{LL temp=dfs (len-1, cen,pre+i* (Len-cen), fp && i = =Fpmax); RET+=temp; }   if(!FP) Dp[len][cen][pre]=ret; returnret;} ll F (ll N) {if(n==-1)        return 0; intlen=0;  while(n) {digit[++len] = n%Ten; N/=Ten; } LL ans=0;  for(intcen=1; cen<=len;cen++) {ans+=dfs (Len,cen,0,true); }     returnans-len+1;}voidinit () {memset (DP,-1,sizeof(DP));}intMain () {//freopen ("Test.txt", "R", stdin);    intT; scanf ("%d",&t);  while(t--) {init ();        LL n,m; scanf ("%i64d%i64d",&n,&m); LL ans1=f (m); //init ();LL ans2=f (n1); printf ("%i64d\n", ans1-ans2); }    return 0;}

hdu3709 (balanced number) digital DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.