题目背景
(本道题目木有以藏歌曲……不用猜了……)
《爱与愁的故事第一弹·heartache》最终章。
吃完pizza,月落乌啼知道超出自己的预算了。为了不在爱与愁大神面前献丑,只好还是硬着头皮去算钱……
题目描述
算完钱后,月落乌啼想着:“你TMD坑我,(以下用闽南语读)归粒靠杯靠亩诶,(以下用英读)是伊特游!”于是当爱与愁大神问多少钱时,月落乌啼说了一堆乱码。爱与愁大神说:“算了算了,我只问第n样菜价格多少?”月落乌啼写出了:
。由于爱与愁大神学过编程,于是就用1分钟的时间求出了Fn的结果。月落乌啼为此大吃一惊。你能学学爱与愁大神求出Fn的值吗?
输入格式
只有1行:n
输出格式
只有1行:Fn,保留两位小数。
输入输出样例
输入 #1
6
输出 #1
8.00
说明/提示
所有数据:n<=48
如果不知道怎么做,可以先计算出前几项,然后找规律。
本文多种解法来源AceCandy,感谢!详细解法说明请见以上链接。
import java.text.DecimalFormat;
import java.util.Scanner;
public class P1720 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
DecimalFormat df = new DecimalFormat("0.00");
System.out.print(df.format(f(n)));
}
public static long f(int n) {
double result = 0;
double temp = Math.sqrt(5.0);
result = (Math.pow((1 + temp) / 2, n) - Math.pow((1 - temp) / 2, n)) / temp;
return (long) result;
}
}
public static long f1(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return 1;
}
return f1(n - 2) + f1(n - 1);
}
static HashMap<Integer, Long> map = new HashMap<>();
public static long f2(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return 1;
}
if (!map.containsKey(n)) {
map.put(n, f2(n - 2) + f2(n - 1));
}
return map.get(n);
}
static long[] mArray = new long[8000 + 1];
public static long f3(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return mArray[n] = 1;
}
if (mArray[n] == 0) {
mArray[n] = f3(n - 1) + f3(n - 2);
}
return mArray[n];
}
public static long f4(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return 1;
}
long temp[] = new long[n + 1];
temp[0] = 0;
temp[1] = 1;
temp[2] = 1;
for (int i = 3; i <= n; ++i) {
temp[i] = temp[i - 1] + temp[i - 2];
}
return temp[n];
}
public static long f5(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return 1;
}
long a = 1;
long b = 1;
long c = 0;
for (int i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
static long[][] initMatirx = {{1, 1}, {1, 0}};
public static long f6(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return 1;
}
long[][] tem = initMatirx;
for (int i = 1; i < n - 2; i++) {
tem = matirxMulti(tem, initMatirx);
}
return tem[0][0] + tem[1][0];
}
private static long[][] matirxMulti(long[][] a, long[][] b) {
long[][] temp = new long[2][2];
temp[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0];
temp[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1];
temp[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0];
temp[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1];
return temp;
}
static long[][] initMatirx = {{1, 1}, {1, 0}};
static long[][] unitMatrix = {{1, 0}, {0, 1}};//单位矩阵
public static long f7(int n) {
if (n <= 0) {
throw new RuntimeException("输入参数小于1");
}
if (n == 1 || n == 2) {
return 1;
}
long[][] result = unitMatrix;
long[][] tem = initMatirx;
int m = n - 2;
while (m != 0) {
if ((m & 1) == 1) {
result = matirxMulti(tem, result);
}
tem = matirxMulti(tem, tem);
m >>= 1;
}
return result[0][0] + result[1][0];
}