hdu 6093 数位暴力

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static BigInteger[] bg;
	public static BigInteger[] jc;
	public static boolean[] f;
	
	public static int jud(BigInteger bi) {
		int l = 0, r = 1600;
		while(r - l > 0) {
			int mid = (l + r) >> 1;
			if(bi.compareTo(bg[mid]) > 0) {
				l = mid + 1;
			} else {
				r = mid;
			}
		}
		return r;
	}
	public static void init() {
		bg = new BigInteger[1600];
		bg[0] = BigInteger.ZERO;
		bg[1] = BigInteger.ZERO;
		BigInteger tmp;
		for(int i=2; i<1600; ++i) {
			tmp = BigInteger.valueOf(i);
			bg[i] = BigInteger.ZERO;
			int j = i - 1;
			while(j >= 0) {
				 bg[i] = bg[i].multiply(tmp).add(BigInteger.valueOf((j)));
				 --j;
			}
		}
		jc = new BigInteger[1600];
		jc[0] = BigInteger.ONE;
		jc[1] = BigInteger.ONE;
		for(int i=2; i<1600; ++i) {
			jc[i] = jc[i - 1].multiply(BigInteger.valueOf(i));
		}
		f = new boolean[1600];
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in 

);
		BigInteger mod = BigInteger.valueOf(998244353);
		init();
		int t;
		BigInteger L,R, ans;
		t = sc.nextInt();
		while(t-- > 0) {
			ans = BigInteger.ZERO;
			L = sc.nextBigInteger();
			R = sc.nextBigInteger();
			for(int i=0; i<1600; ++i) {
				f[i] = false;
			}
			int l = jud(L);
			int r = jud(R);
			if(r - l > 1) {
				ans = ans.add(jc[r - 1].subtract(jc[l]));
			}
			BigInteger tmp = BigInteger.valueOf(l).pow(l - 1);
			for(int i=l; i>0; --i) {
				int now = Integer.valueOf(L.divide(tmp).toString());
				if(now == 0 && i == l) {
					ans = ans.add(jc[l].subtract(jc[l - 1]));
					break;
				}
				int cnt = 0;
				for(int j=now + 1;j0; --i) {
				int now = Integer.valueOf(R.divide(tmp).toString());
				if(now == 0 && i == r) {
					res = res.add(jc[r].subtract(jc[r - 1]));
					break;
				}
				int cnt = 0;
				for(int j=now + 1;j

你可能感兴趣的:(hdu 6093 数位暴力)