第一题:受伤的皇后
补题中-----------------------------,补完放评论区
第二题:完全平方数
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
Scanner s = new Scanner(System.in);
long n = s.nextLong();
long res = 1;
for(int i = 2; i <= n/i; i++) {
int cnt = 0;
while(n % i == 0) {
cnt++;
n /= i;
}
if(cnt % 2 == 1) res *= i;
}
if(n > 1) res *= n;
System.out.println(res);
}
}
第三题:123
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int N = 1500000+10;
static long[] s = new long[N];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
long sum = 0;
for(int i = 1; i <= N-10; i++){
sum += i;
s[i] += s[i-1] + sum;
}
while(t-- > 0){
String[] str = br.readLine().split(" ");
long a = Long.parseLong(str[0]);
long b = Long.parseLong(str[1]);
System.out.println(solve(b) - solve(a-1));
}
}
public static long solve(long n){
int l = 1, r = N-10;
while(l < r){
int mid = (l + r) >> 1;
if(f(mid) < n){
l = mid + 1;
}else{
r = mid;
}
}
r--;
n -= f(r);
return s[r] + f(n);
}
public static long f(long x){
return (1L + x) * x / 2;
}
}
第四题:求阶乘
import java.util.Scanner;
public class Main {
public static long getFive(long n){
long sum = 0;
int t = 5;
while(n > 0){
sum += n / t;
n /= t;
}
return sum;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
long k = s.nextLong();
long l = 1, r = (long)1e20;
while(l < r){
long mid = l + (r-l)/2;
if(getFive(mid) >= k) r = mid;
else l = mid+1;
}
System.out.println(getFive(r) == k?r: -1);
}
}