http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2313
要想满足要求 k 必须和 n 是互质的
当球第一次回到起点是 球走过的路程是 k和n 的最小公倍数
如果 k 和 n 互质 那么球正好传递了 n 次 正好每个人都传了一次
如果 k 和 n 不互质 就不能满足条件了
从 n/2 开始搜 很快就可以搜到和 n 互质的数
代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int T;
T = in.nextInt();
while ((T--) > 0) {
BigInteger a = in.nextBigInteger();
BigInteger b = a.divide(BigInteger.ONE.add(BigInteger.ONE));
while (b.compareTo(BigInteger.ZERO) == 1) {
if (a.gcd(b).compareTo(BigInteger.ONE) == 0) {
break;
}
b = b.subtract(BigInteger.ONE);
}
System.out.println(b);
if (T > 0) {
System.out.println();
}
}
}
}