package 蓝桥;
import java.util.Scanner;
//123
public class Test01_2_14_123 {
static long[] s = new long[1500010];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long t = 0;
for (int i = 1; i <= 1500000; i++) {
t += i;
s[i] = s[i - 1] + t;
}
int n = sc.nextInt();
while (n-- > 0) {
long a = sc.nextLong(), b = sc.nextLong();
System.out.println(slove(b) - slove(a - 1));
}
}
//求前x个元素的前缀和
private static long slove(long x) {
int l = 1, r = 1500000;
while (l < r) {// 二分法快速求得 x 位于 sum 中的哪一块 -- 解题关键
int mid = l + r >> 1;
if (f(mid) < x)
l = mid + 1;
else
r = mid;
}
r--;// 求得前面块
x -= f(r);
return s[r] + f(x);
}
private static long f(long x) {// 等差数列求和
return (1L + x) * x / 2;
}
}
123
求阶乘
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long in = sc.nextLong();
long l = 1,r=(long)1e20;
while(l<r) {
long mid = l+(r-l)/2;
if(f2(mid)>=in)r=mid;
else l = mid + 1;
}
long x = f2(r);
System.out.println(x==in?r:-1);
// System.out.println(f2(210));
sc.close();
}
public static long f2(long n) {
long ans=0;
while(n>0) {
ans = ans + n/5;
n/=5;
}
return ans;
// System.out.println(ans);
}
public static void f1(int n) {
BigInteger a = new BigInteger("1");
for(int i = 1;i<=n;i++) {
a=a.multiply(BigInteger.valueOf(i));
}
String s = a.toString();
System.out.println(s);
int count=0;
for(int i =s.length()-1;i>=0;i--) {
if(s.charAt(i)!='0')break;
count++;
}
System.out.println(count);
}
}