A
(long)sqrt(N) * 2 + 1
B
N / 100 * 2
什么环来着
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
String[] s = new String[n + 1];
int[][] g = new int[n + 1][n + 1];
for(int i = 1; i <= n; i++){
s[i] = scan.next();
s[i] = s[i] + s[i];
}
// 构建完整的带权图
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
g[i][j] = g[j][i] = Math.min(m, work(s[i], s[j]));
}
}
// 使用Prim算法找到最大权值的最小生成树
int[] mx = new int[n + 1];
boolean[] st = new boolean[n + 1];
Arrays.fill(mx, Integer.MIN_VALUE);
mx[1] = 0;
int res = 0;
for(int i = 1; i <= n; i++){
int idx = -1;
for(int j = 1; j <= n; j++){
if(!st[j] && (idx == -1 || mx[j] > mx[idx])){
idx = j;
}
}
st[idx] = true;
res += mx[idx];
for(int j = 1; j <= n; j++){
if(!st[j] && g[idx][j] > mx[j]){
mx[j] = g[idx][j];
}
}
}
System.out.println(res);
}
public static int work(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int[][] dp = new int[m + 1][n + 1];
int maxLength = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > maxLength) {
maxLength = dp[i][j];
}
} else {
dp[i][j] = 0;
}
}
}
return maxLength;
}
}
博弈
import java.util.*;
public class Main{
static int N = (int) 1e5 + 10;
static int[] prime = new int[N];
static int cnt = 0;
static boolean[] st = new boolean[N];
static int[] res = new int[N];
static void init(){
for(int i = 2; i < N; i++){
if(!st[i]) prime[cnt++] = i;
for(int j = 0; j < cnt && prime[j] * i < N; j++){
st[prime[j] * i] = true;
}
}
for(int i = 0; i < cnt; i++){
res[prime[i]] = res[prime[i] + 1] = 1;
}
for(int i = 2; i < N; i++){
for(int j = 0; j < cnt && prime[j] <= i; j++){
if(res[i - prime[j]] == 0) {
res[i] = 1;
break;
}
}
}
// for(int i = 0; i < 100; i++){
// System.out.print(res[i] + " ");
// }
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
init();
int q = scan.nextInt();
for(int i = 0; i < q; i++){
int n = scan.nextInt();
System.out.println(res[n]);
}
}
}
删什么玩意来着
import java.util.*;
public class Main{
static boolean check(long a){
long l = 1, r = (long) 1e9;
while(l <= r){
long mid = l + r >> 1;
long sum = 0;
if(mid % 2 == 0){
sum = (1 + mid) * (mid / 2);
}else{
sum = (1 + mid) * (mid / 2) + (mid + 1) / 2;
}
if(sum == a){
return true;
}else if(sum > a){
r = mid - 1;
}else{
l = mid + 1;
}
}
return false;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int res = 0;
for(int i = 0; i < n; i++){
long a = scan.nextLong();
if(!check(a)){
res++;
}
}
System.out.println(res);
}
}
什么回文数操作
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
long[] b = new long[n];
for(int i = 0; i < n; i++){
a[i] = scan.nextInt();
}
for(int i = 0, j = n - 1; i < j; i++, j--){
b[i] = a[j] - a[i];
}
long res = 0;
for(int i = 0; i < n / 2; i++){
res += Math.abs(b[i]);
if(b[i] * b[i + 1] > 0){
if(b[i] > 0) {
b[i + 1] = Math.max(0L, b[i + 1] - b[i]);
}else{
b[i + 1] = Math.min(0L, b[i + 1] - b[i]);
}
}
}
System.out.println(res);
}
}
最后一题太难了,没看,还有一道题忘了题目了,写7个小菜