[Digital DP] lightoj 1205 palindromic numbers

Source: Internet
Author: User

Question: The number of replies in a given range.

Ideas:

The URL Length of DP [site] [Len] site bit is Len.

You need an array to store the number of each row.

Then there is DFS, and the middle value of Len is mid.

If the value exceeds the mid value, check the symmetric number in OK to determine whether it is equal.

Can be placed in the next place.

Note that the leading 0 and the OJ must use long.

Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "queue" # include "algorithm" # include "iostream" using namespace STD; // September 24, 2014 long DP [22] [22]; int num [22], OK [22]; long DFS (INT site, int N, int zero, int f) {If (Site = 0) return 1; if (! F &&! Zero & DP [site] [N]! =-1) return DP [site] [N]; int Len = f? Num [site]: 9; long ans = 0; For (INT I = 0; I <= Len; I ++) {If (zero) {if (I = 0) ans + = DFS (site-1, n-1, zero & I = 0, F & I = Len ); else {OK [site] = I; ans + = DFS (site-1, n, zero & I = 0, F & I = Len );}} else {int mid = (n + 1)/2; If (N % 2) {If (Site = mid) ans + = DFS (site-1, n, zero & I = 0, F & I = Len); else if (site> mid) {OK [site] = I; ans + = DFS (site-1, n, zero & I = 0, F & I = Len );} else {If (OK [n + 1-site] = I) ans + = DFS (site-1, n, zero & I = 0, F & I = Len) ;}} else {If (site> mid) {OK [site] = I; ans + = DFS (site-1, n, zero & I = 0, F & I = Len);} else {If (OK [n + 1-site] = I) ans + = DFS (site-1, n, zero & I = 0, F & I = Len) ;}}} if (! F &&! Zero) DP [site] [N] = ans; return ans;} long solve (long x) {int CNT = 0; while (X) {num [++ CNT] = x % 10; X/= 10;} return DFS (CNT, CNT, 1,1);} int main () {int t, CAS = 1; CIN> T; memset (DP,-1, sizeof (DP); While (t --) {long X, Y; scanf ("% LLD", & X, & Y); If (x> Y) Swap (x, y); printf ("case % d: % LLD \ n ", CAS ++, solve (y)-solve (x-1);} return 0;} // September 24, 2014 11:26:45


[Digital DP] lightoj 1205 palindromic numbers

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.