Codeforces 55D Beautiful Numbers digital DP (Getting Started

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

We think that a number num can be divisible by the number on each (expect 0) then this number is legal.

Give the interval [l,r] and ask how many valid numbers are in the interval.

First solve (long x) returns the number of legal numbers in [0, X], the answer is solve (r)-Solve (L-1);

Take 1234567 as an example

Flag indicates whether the current one can fill in, or can only fill in <= the corresponding number of digits

If the current search is third, and the second has been filled with 0 or 1, then the third place can be filled in at this time.

If the second fill is 2, then the third person can only fill in [0, 3], so DFS when a token, mark whether this can be filled.


If the current search is in the I-bit, it is clear that PRE_LCM and pre_mod are [highest bit, i+1] mod values and LCM


The function of Pre_mod is to determine whether the N-bit LCM is divisible by filling all the numbers (n-digits are filled).

If divisible, num = Lcm*x (and LCM is the largest LCM (1,2 9) = 2520, so 2520%LCM = = 0)

Set num = X*2520+y, then NUM%LCM = (x*2520+y)%LCM = Y%LCM

=>num%LCM = NUM%2520%LCM


And 2520 of all the factors may be 2~9 combination of common multiple, (of course, only part of the factor) Init () discretization of all the factors of 2520

DP[POS][PRE_MOD][PRE_LCM] represents the number of pre-POS digits to 2520 for Pre_mod and non-0-bit LCM bits PRE_LCM.

And the answer is the lowest back, so you can memorize it.


Import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.io.printwriter;import Java.math.biginteger;import jav<span style= "Background-color:rgb (255, 255, 255); > Xcode </span><span style= "font-family:arial, Helvetica, Sans-serif;" >a.text.DecimalFormat;</span>
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 {int[] bit = new INT[30], hash = new int[2550];long[][][] dp = new long[30][255 0][50];long DFS (int pos, int pre_mod, int pre_lcm, Boolean flag) {if (pos = = 0) return pre_mod%pre_lcm==0?1:0;if (flag &AMP;&A mp DP[POS][PRE_MOD][HASH[PRE_LCM]]!=-1) return dp[pos][pre_mod][hash[pre_lcm]];int U = flag?9:bit[pos];long ans = 0L;for ( int d = 0; D <= u; d++) {int next_mod = (pre_mod*10+d)%mod;int NEXT_LCM = pre_lcm;if (d>0) NEXT_LCM = LCM (PRE_LCM, D); ans + = DFS (pos-1, Next_mod, NEXT_LCM, flag| | D&LT;U);} if (flag) DP[POS][PRE_MOD][HASH[PRE_LCM]] = Ans;return ans;} Long solve (long x) {int len = 0;while (x>0) {Bit[++len] = (int) (x%10l); x/=10l;} Return Dfs (len, 0, 1, false);} void Init () {int cnt = 0;for (int i = 1; I <= mod; i++) if (mod%i==0) hash[i]=++cnt;for (int i = 0; i <; i++) for (Int J = 0; J < 2520; J + +) for (int k = 0; k <; k++) dp[i][j][k] = 1;} void work () throws Exception{init (), int T = Int (), while (t-->0) {Long L = long (), R = Long (), Out.println (Solve (R)-Solve    (l-1l));}}    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 = 3*100050;static int M = n*n * 10;D Ecimalformat df=new DecimalFormat ("0.0000"); static long inf = 1000000000000l;static long inf64 = (long) 1e18*2;static do  uble 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 = m ID + 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;} 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);}}


Codeforces 55D Beautiful Numbers digital DP (Getting Started

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.