SPOJ MYQ10 Mirror number digit DP '

Source: Internet
Author: User

Topic Link: Click to open the link

Myq10-mirror number

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 are a mirror number while are not.

Given numbers A and B, find the number of mirror numbers in between them (inclusive of A and B).

Input

First line contains T, number of testcases <= 10^5.
Each testcase are described in a, containing, and b numbers a.

0 <= a<=b <= 10^44

Output

For each test case, print the number of mirror numbers between A and b in a.

Example
Input:3
0 10
10 20
1 4Output:3
1
1

Test instructions

Given an interval [l,r] The number of questions in the interval is central symmetry.

First, the number of symmetrical numbers must only be composed of 0 1 8.

Dp[cur][start][flag] represents the number of start, has looked for the start-cur+1 bit, is or is not the number of mirrored symmetric numbers


Import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.math.biginteger;import Java.text.decimalformat;import Java.util.arraydeque;import Java.util.ArrayList;import Java.util.arrays;import Java.util.collections;import Java.util.comparator;import Java.util.Deque;import Java.util.hashmap;import Java.util.iterator;import Java.util.linkedlist;import Java.util.Map;import Java.util.priorityqueue;import Java.util.scanner;import Java.util.stack;import Java.util.StringTokenizer;import Java.util.treemap;import Java.util.treeset;import Java.util.queue;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;public class Main { Boolean three (char c) {return c== ' 0 ' | | c== ' 1 ' | | c== ' 8 ';} Boolean three_num (int c) {return c==0| | c==1| | C==8;} int[] num = new Int[n], TMP = new int[n];long[][][] dp = new long[n][n][2];//cur: Current digit, start: Mirror palindrome judgment start place, flag: Whether it is a mirror palindrome, limit : Boundary judge Long dfs (int cur, int start, int flaG, Boolean limit) {if (cur==-1) return flag;if (!limit && dp[cur][start][flag]! =-1) return Dp[cur][start][flag]; Long ans = 0;int end = limit?num[cur]:9;for (int i = 0; I <= end i++) if (Three_num (i)) {Boolean St = (cur = = Start && Amp i = = 0); int newflag = FLAG;IF (Flag > 0) {if (!st && cur< (start+1)/2) Newflag = (Tmp[start-cur] = = i)? 1:0;} Tmp[cur] = I;ans + = DFS (cur-1, St?start-1:start, Newflag, limit&& (I==end));} if (!limit) dp[cur][start][flag] = Ans;return ans;} Long solve (String x) {for (int i = 0; i < x.length (); i++) Num[i] = X.charat (X.length () -1-i)-' 0 '; num[x.length ()] = 0;ret Urn Dfs (X.length ()-1, X.length ()-1, 1, true);} void work () throws exception{for (int i = 0; i < N; i++) for (int j = 0; J < N; J + +) Arrays.fill (Dp[i][j], 1); int T = i NT (), while (t-->0) {String L = Next (), R = Next (), long ans = 1;for (int i = 0; i < l.length (); i++) if (!three (L.charat (i )) ||  L.charat (i)!=l.charat (L.length () -1-i)) ans = 0l;out.println ((Solve (R)-solve (L) +ans));}}  public static void Main (string[] args) throws exception{main wo = new main ();    in = new BufferedReader (new InputStreamReader (system.in));  out = new PrintWriter (System.out);  in = new BufferedReader (new InputStreamReader (New FileInputStream (New File ("Input.txt")));        out = new PrintWriter (New File ("output.txt"));        Wo.work ();    Out.close (); }static int N = 50;static int M = n*n * 10;decimalformat df=new DecimalFormat ("0.0000"); static long inf = 1000000000000l;s  Tatic Long inf64 = (long) 1e18*2;static double EPS = 1e-8;static double Pi = math.pi;static int mod = 2520;p rivate String Next () throws exception{while (str = = NULL | |!str.hasmoreelements ()) str = new StringTokenizer (In.readline ())    ;    return Str.nexttoken ();    } private int int () throws exception{return Integer.parseint (Next ());    } Private Long Long () throws exception{return Long.parselong (Next ());    } StringTokenizer str;    Static BufferedReader in; Static PrintWriter out; /*class Edge{int from, to, NEX; Edge () {}edge (int from, int. to, int nex) {This.from = From;this.to = To;this.nex = NEX;}} Edge[] Edge = new edge[m<<1];int[] head = new Int[n];int edgenum;void Init_edge () {for (int i = 0; i < N; i++) head[ I] =-1; Edgenum = 0;} void Add (int u, int v) {Edge[edgenum] = new Edge (U, V, head[u]); Head[u] = edgenum++;} /**/int Upper_bound (int[] A, int l, int r, int val) {//Upper_bound (a+l,a+r,val)-a;int pos = R;r--;while (l <= r) {int Mid = (L + R) >> 1;if (A[mid] <= val) {L = mid + 1;} else {pos = Mid;r = Mid-1;}} return POS;} int Pow (int x, int y) {int ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} Double Pow (double x, int y) {double ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} int pow_mod (int x, int y, int Mod) {int ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;ans%= mod;y >>= 1 ; x = x * x;x%= mod;} return ans;}long Pow (long x, long y) {long ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;y >>= 1;x = x * x;} return ans;} Long Pow_mod (long x, long y, long Mod) {Long ans = 1;while (Y > 0) {if ((Y & 1) > 0) ans *= x;ans%= mod;y &GT;&G t;= 1;x = x * x;x%= mod;} return ans;} int Gcd (int x, int y) {if (x>y) {int tmp = x; x = y; y = tmp;} while (x>0) {y%= x;int tmp = x; x = y; y = tmp;} return y;} Long GCD (long x, long y) {if (x>y) {long tmp = x; x = y; y = tmp;} while (x>0) {y%= x;long tmp = x; x = y; y = tmp;} return y;} int Lcm (int x, int y) {return x/gcd (x, y) *y;} Long Lcm (long x, long y) {return x/gcd (x, y) *y;} int max (int x, int y) {return x > y x:y;} int min (int x, int y) {return x < y x:y;} Double Max (double x, double y) {return x > y x:y;} Double min (double x, double y) {return x < y x:y;} Long Max (long x, long y) {return x > y x:y;} Long min (long x, long y) {return x < y x:y;} int abs (int x) {return x > 0 ×:-X;} Double ABS (Double X) {return x > 0? x:-X;} Long ABS (long x) {return x > 0 ×:-X;} Boolean zero (double x) {return abs (x) < EPS;} Double sin (double x) {return Math.sin (x);} Double cos (double x) {return math.cos (x);} Double tan (double x) {return Math.tan (x);} Double sqrt (double x) {return math.sqrt (x);}}



SPOJ MYQ10 Mirror number digit 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.