HDOJ 3709 Balanced Number
生活随笔
收集整理的這篇文章主要介紹了
HDOJ 3709 Balanced Number
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數位DP。。。
Total Submission(s): 1337????Accepted Submission(s): 583
Problem DescriptionA 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].
InputThe 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).
OutputFor each case, print the number of balanced numbers in the range [x, y] in a line.
Sample Input2
0 9
7604 24324
Sample Output10
897
AuthorGAO, Yuan
Source2010 Asia Chengdu Regional Contest
Recommendzhengfeng?
* This source code was highlighted by?YcdoiT. ( style: Codeblocks )
Balanced Number
Time Limit: 10000/5000 MS (Java/Others)????Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1337????Accepted Submission(s): 583
Problem DescriptionA 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].
InputThe 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).
OutputFor each case, print the number of balanced numbers in the range [x, y] in a line.
Sample Input2
0 9
7604 24324
Sample Output10
897
AuthorGAO, Yuan
Source2010 Asia Chengdu Regional Contest
Recommendzhengfeng?
| #include?<iostream> #include?<cstdio> #include?<cstring> using?namespace?std; typedef?long?long?int?LL; LL?x,y,dp[20][20][2000]; int?bit[20]; LL?dfs(int?pos,int?o,int?sum,int?limit) { ????if(sum<0)?return?0; ????if(pos==-1)?return?sum==0; ????if(!limit&&~dp[pos][o][sum])?return?dp[pos][o][sum]; ????int?end=limit?bit[pos]:9; ????LL?ans=0; ????for(int?i=0;i<=end;i++) ????{ ????????ans+=dfs(pos-1,o,sum+i*(pos-o),limit&&i==end); ????} ????if(!limit) ????????dp[pos][o][sum]=ans; ????return?ans; } LL?calu(LL?x) { ????int?pos=0; ????while(x) ????{ ????????bit[pos++]=x%10; ????????x/=10; ????} ????LL?ans=0; ????for(int?o=0;o<pos;o++) ????{ ????????ans+=dfs(pos-1,o,0,true); ????} ????return?ans-pos; } int?main() { ????int?t; ????scanf("%d",&t); ????memset(dp,-1,sizeof(dp)); ????while(t--) ????{ ????????scanf("%I64d%I64d",&x,&y); ????????printf("%I64d\n",calu(y)-calu(x-1)); ????} ????return?0; } |
轉載于:https://www.cnblogs.com/CKboss/p/3350826.html
總結
以上是生活随笔為你收集整理的HDOJ 3709 Balanced Number的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#获取数据库表信息,列信息
- 下一篇: javascript 注意事项