Spoj-myq10 mirror number (Digital DP)

Source: Internet
Author: User

Description

A number is called a mirror number if on lateral inversion, it gives the same number I. e it looks the same in a mirror. For example 101 is a mirror number while 100 is not.

Given two numbers a and B, find the number of mirror numbers in between them (random sive of A and B ).

Input

First line contains t, number of testcases <= 10 ^ 5.
Each testcase is described in a single line containing two numbers A and B.

0 <= A <= B <= 10 ^ 44

Output

For each test case print the number of mirror numbers between A and B in a single line.

Example
Input: 30 1010 201 4 Output: 311

Question: The number of image replies is in the range of [a, B ].

Idea: the relatively difficult digital DP, compared with the common digital DP, has two more parameters: one is the starting position, and the other is to judge whether the image is retrieved,

Use a tmp to store the numbers that have already been stored to determine if it is a text return.

# Include <cstdio> # include <algorithm> # include <cstring> # include <iostream> using namespace STD; typedef long ll; const int maxn = 50; ll DP [maxn] [maxn] [2]; char num [maxn]; int TMP [maxn]; // cur: Current digit, start: Start of image retrieval judgment, flag: indicates whether the image is returned. Limit: determines the ll DFS (INT cur, int start, bool flag, bool limit) {If (cur =-1) return flag; If (! Limit & DP [cur] [start] [flag]! =-1) return DP [cur] [start] [flag]; ll ans = 0; int end = limit? (Num [cur]-48): 9; For (INT I = 0; I <= end; I ++) {if (I = 0 | I = 1 | I = 8) {bool ST = (cur = Start & I = 0 ); // determine whether it is 0. bool newflag = flag; If (FLAG) {If (! St & cur <(start + 1)/2) newflag = (TMP [start-cur] = I);} TMP [cur] = I; ans + = DFS (cur-1, st? Start-1: Start, newflag, limit & (I = END) ;}} if (! Limit) DP [cur] [start] [flag] = ans; return ans;} ll CAL (char STR []) {int Len = strlen (STR ); for (INT I = 0; I <Len; I ++) num [I] = STR [len-1-i]; num [Len] = 0; return DFS (len-1, len-1, 1, 1) ;}int main () {int t; char a [maxn], B [maxn]; scanf ("% d", & T); getchar (); memset (DP,-1, sizeof (DP); While (t --) {scanf ("% S % s", a, B); LL ans = CAL (B) -Cal (a); int Len = strlen (a); bool flag = true; For (INT I = 0; I <Len; I ++) If (A [I]! = '0' & A [I]! = '1' & A [I]! = '8') | (A [I]! = A [len-1-i]) {flag = false; break;} If (FLAG) ans ++; printf ("% LLD \ n", ANS);} return 0 ;}



Spoj-myq10 mirror 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.