Codeforces 55D Beautiful numbers 数位dp(入门

题目链接:点击打开链接

题意:

我们认为一个数 num 能被每一位上的数字整除(expect 0) 那么这个数num就是合法的。

给出区间[l,r] ,问这个区间内有多少个合法的数。

首先solve(long x) 返回 [0, x] 内的合法个数,答案就是 solve(r) - solve(l-1);

以1234567为例

flag表示当前这位是能任意填,还是只能填<=该位对应的数字

若当前搜索的是第三位,且第二位已经填了0或1,则此时第三位可以任意填。

若第二位填的是2,则第三位只能填 [0, 3] ,所以dfs时传一个标记,标记这位是否能随便填。


若当前搜索的是第i位,显然 pre_lcm和pre_mod 就是[最高位, i+1] 的mod值和lcm


pre_mod的作用是在填充完所有数字(n位数字都填充完)判断能否被这n位的lcm整除,

若能整除则 num = lcm*x (而lcm最大是 lcm(1,2··9) = 2520,所以2520%lcm == 0)

设num = x*2520+y,则 num%lcm = (x*2520+y)%lcm = y%lcm 

=>num %lcm = num%2520%lcm


而2520的所有因子都有可能是2~9的组合的公倍数,(当然只是部分的因子)Init()离散化一下2520的所有因子 

dp[pos][pre_mod][pre_lcm]代表前pos位数对2520取余为pre_mod并且非零位的lcm位pre_lcm的个数。

而答案是最低位往上回溯的,所以可以记忆化


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][2550][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 && 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<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 < 30; i++)for(int j = 0; j < 2520; j++)for(int k = 0; k < 50; 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;
	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)-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 >>= 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);}
}


你可能感兴趣的:(Codeforces 55D Beautiful numbers 数位dp(入门)