AcWing 3418. 杨辉三角形
原题链接
中等
JAVA 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
work(n);
}
static void work(int n) {
long[][] f = new long[2001][2001];
f[1][1] = 1;
if (n == 1) {
System.out.println(1);
return;
}
for (int i = 2; i <= 2000; i++) {
for (int j = 1; j <= i; j++) {
f[i][j] = f[i - 1][j] + f[i - 1][j - 1];
if (f[i][j] == n) {
System.out.println(1l * i * (i - 1) / 2 + j);
return;
}
}
}
long sqrt = (long) Math.sqrt(n * 2);
if (sqrt * (sqrt + 1) == 2 * n) {
System.out.println(1l * (sqrt+2) * (1 + sqrt) / 2 + 3);
return;
}
System.out.println(1l * n * (n + 1) / 2 + 2);
}
}