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;j<l; ++j) {
if(f[j] != true) {
++cnt;
}
}
ans = ans.add(jc[i - 1].multiply(BigInteger.valueOf(cnt)));
if(i == 1 && f[now] == false) {
ans = ans.add(BigInteger.ONE);
}
if(f[now] == true) {
break;
}
f[now] = true;
L = L.mod(tmp);
tmp = tmp.divide(BigInteger.valueOf(l));
}
for(int i=0; i<1600; ++i) {
f[i] = false;
}
BigInteger res = BigInteger.ZERO;
tmp = BigInteger.valueOf(r).pow(r - 1);
for(int i=r; i>0; --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<r; ++j) {
if(f[j] != true) {
++cnt;
}
}
res = res.add(jc[i - 1].multiply(BigInteger.valueOf(cnt)));
if(f[now] == true) {
break;
}
f[now] = true;
R = R.mod(tmp);
tmp = tmp.divide(BigInteger.valueOf(r));
}
if(l == r) {
ans = ans.subtract(res);
} else {
res = jc[r].subtract(jc[r - 1]).subtract(res);
ans = ans.add(res);
}
System.out.println(ans.mod(mod).toString());
}
}
}
hdu 6093 数位暴力
最新推荐文章于 2018-08-05 23:36:39 发布