SPOJ MYQ10 Mirror Number digital dp & #39;
Question link: Click the open 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 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 4Output:311
Question:
Given an interval [l, r], the number of numbers in the interval is symmetric.
First, the symmetric number must be composed of 0 1 8.
Dp [cur] [start] [flag] indicates the number with a length of start. We have searched for the start-cur + 1 digit, which is or is not the number of image 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. using list; import java. ut Il. map; import java. util. priorityQueue; import java. util. imports; 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_nu M (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: image Retrieval judgment start point: flag: whether it is image retrieval, limit: boundary judgment 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 & 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; return 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 = Int (); 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 * 10; DecimalFormat df = new DecimalFormat ("0.0000"); static long inf = 1000000000000L; static long inf64 = (long) 1e18*2; static double eps = 1e-8; static double Pi = Math. PI; static int mod = 2520; private 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)-; 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> = 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:-x;} double abs (double x) {return x> 0? X:-x;} long abs (long x) {return x> 0? X:-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 );}}