hdu3709balanced number Digit DP

Source: Internet
Author: User

Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.a.q
Hand in Hand
Online acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors ranklist
C/c++/java Exams
ACM Steps
Go to Job
Contest Livecast
Icpc@china
Best Coder Beta
VIP | STD Contests
Virtual Contests
DIY | Web-diy Beta
Recent contests
20130738
Mail 0 (0)
Control Panel
Sign Out
Enrollment--Hangzhou Electric "Trinity" all Raiders.
Recruitment--Bakassi technology (Hangzhou), Hero Mutual Entertainment (Hangzhou)
Balanced Number Time limit:10000/5000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 3549 Accepted Submission (s): 1630


Problem Description A 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].
Input the 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≤10 18).
Output for each case, print the number of balanced numbers in the range [x, y] with a line.
Sample Input
2 0 9) 7604 24324
Sample Output
10 897
Author GAO, Yuan
Source of Asia Chengdu regional Contest

Hold a noon only a dropped the question = = also is a digital DP in the more classic

Say test instructions: Given a known number string, requiring a number to be centered on both sides of the moment and the number of equal, we consider three results related to the amount: the length of the current enumeration, the position of the axis, the moment and, respectively, as a DP of three-dimensional

Flag to mark whether the maximum bit is used, notice sum<0 exit; pos=0, if Sum=0 then the number of methods equal to +1, because the requirement is ==0, that is, in the process of recursion, POS center of the relative position is uncertain, POS move to the right is still pos-center so is added negative value = = to the last flag=0, we can assume that the current situation is completed before the DP can be assigned

Understand this problem well to make it to the next poj3252--

/************ hdu3709 2016.3.11 31MS 22860K 1075 B C + + ************/#include <iostream> #include <cstdio> #inc
Lude<cstring> using namespace std;
Long Long dp[30][30][3000];
int num[30];
    Long Long dfs (int pos,int center,int Sum,bool flag) {if (pos==0) return sum==0;
    if (sum<0) return 0;
    if (!flag&&dp[pos][center][sum]!=-1) return dp[pos][center][sum];
    Long Long ans=0;
    int maxn=flag?num[pos]:9;
    for (int i=0;i<=maxn;i++) {Ans+=dfs (pos-1,center,sum+i* (pos-center), I==maxn&&flag);
    }//pos-center if (!flag) Dp[pos][center][sum]=ans;
return ans;
    } A long long cal (Long long x) {long long ans=0;
    int pos=0;
        while (x) {num[++pos]=x%10;
    x/=10;
    } for (int i=1;i<=pos;i++) Ans+=dfs (pos,i,0,1);
return ans-pos+2;
    } int main () {//freopen ("Cin.txt", "R", stdin);
    int t;
    Long Long n,m;
    Memset (Dp,-1,sizeof (DP));
    scanf ("%d", &t);
      while (t--) {  scanf ("%i64d%i64d", &n,&m);
    printf ("%i64d\n", Cal (M)-cal (n-1));
} return 0; }


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.