AcWing 780. 爱健身的小王
原题链接
简单
作者:
莫里Mori
,
2021-04-05 15:50:53
,
所有人可见
,
阅读 270
1. 暴力
import java.util.*;
public class Main{
static int N = 1010;
static int[] h = new int[4010];
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i < t; i ++){
int n = sc.nextInt();
Arrays.fill(h, 0);
int k = 0;
int cnt = 0;
while(h[k] == 0){
h[k] = 1;
k = (k + n + 1) % (4 * n);
cnt ++;
}
System.out.println(cnt + 1);
}
}
}
2. gcd
import java.util.*;
public class Main{
static int N = 1010;
static int[] h = new int[4010];
public static int gcd(int a, int b){
return b == 0? a: gcd(b, a % b);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i < t; i ++){
int n = sc.nextInt();
int d = gcd(4 * n, n + 1);
System.out.println(4 * n / d + 1);
}
}
}