CSP.ac #61乘积求和
题面大意:
#61乘积求和
如果看不了请登入,如果还看不了就是你没去qbxt
solution
帕斯卡法则:
\(\dbinom{n - 1}{k} + \dbinom{n - 1}{k - 1} = \dbinom{n}{k}\)
证明
\(\dbinom{n}{k}\)是从\(n\)中选\(k\)个,有多少种组合
从\(n\)个数中拿出一个数\(x\),有两种情况
\(1.\)选\(x\)的话,那就为\(\dbinom{n - 1}{k - 1}\)
\(2.\)不选\(x\)的话,那就为\(\dbinom{n}{k - 1}\)
两者加和就为\(\dbinom{n}{k}\)
二项式定理
对于一个二项式,\((a+b)^n=\sum\limits_{k = 0}^{n}\dbinom{n}{k}a^{n - k}b^k\)
证明1:
由数学归纳法,当\(n=1\),左边\(=a+b\),右边\(=\dbinom{0}{1}a+\dbinom{1}{1}b\)
设\(n=k\)时该式成立,则\(n=k+1\)时,
\(=(a+b)^n∗(a+b)\)
\(=a∗\sum\limits_{k = 0}^{ n}\dbinom{n}{k}a^{n - k}b^k+b∗\sum\limits_{j = 0}^{ n}\dbinom{n}{j}a^{n - j}b^j\)
\(=\sum\limits_{k = 0}^{n}\dbinom{n}{k}a^{n - k + 1}b^k+\sum\limits_{j = 0}^{n}\dbinom{n}{j}a^{n - j}b^{j + 1}\)
\(=a^{n + 1}\sum\limits_{k = 1}^{n}\dbinom{n}{k}a^{n - k + 1}b^k+\sum\limits_{j = 0}^{n}\dbinom{n}{j}a^{n - j}b^{j + 1}\)(把\(k = 0\)单独取出来)
\(=a^{n + 1}+\sum\limits_{k = 1}^{n}\dbinom{n}{k}a^{n - k + 1}b^k+\sum\limits_{k = 1}^{n + 1}\dbinom{n}{k - 1}a^{n - k + 1}b^{k}\)(令\(j = k - 1\))
\(=a^{n + 1}+\sum\limits_{k = 1}^{n}\dbinom{n}{k}a^{n - k + 1}b^k+b^{n + 1}+\sum\limits_{k = 1}^{n}\dbinom{n}{k - 1}a^{n - k + 1}b^{k}\)(取出第\(k = n + 1\)项)
\(=a^{n + 1}+b^{n + 1}+\sum\limits_{k = 1}^{n}\left(\dbinom{n}{k} + \dbinom{n}{k - 1}\right)a^{n - k + 1}b^{k}\)(加和)
\(=a^{n + 1}+b^{n + 1}+\sum\limits_{k = 1}^{n}\dbinom{n + 1}{k}a^{n - k + 1}b^{k}\)(套用帕卡斯定理)
\(=\sum\limits_{k = 0}^{ n + 1}\dbinom{n + 1}{k}a^{n - k + 1}b^{k + 1}\)(再把k = 0加回去)
证明2:
\((a+b)^n\) 是由\(n\)个\((a+b)\)相乘,对于其中\(a^k\)中的这一项,我们肯定是从中选择了\(k\)个\(a\)相乘,剩下的\(b\)相乘就是\(b^{n−k}\),这样的选法共有\(\dbinom{n}{k}\)个,因此该项为\(\dbinom{n}{k}a^{n - k}b^k\) ,那么\((a+b)^n=\sum\limits_{k = 0}^{n}\dbinom{n}{k}a^{n - k}b^k\)
来自Zsf大佬整理的数学组提供的详细证明2
变形
当\(a = 1\) 时就变成了\((1+b)^n=\sum\limits_{k = 0}^{n}\dbinom{n}{k}b^k\),这里的\(b\)可以为一个式子,既(一会用得到)
\(\left(1 + \dfrac{1 + \sqrt{5}}{2}\right)^n = \sum\limits_{i = 0}^{n}\dbinom{n}{i}\left(\dfrac{1 + \sqrt{5}}{2}\right)^i\)
\(\left(1 + \dfrac{1 - \sqrt{5}}{2}\right)^n = \sum\limits_{i = 0}^{n}\dbinom{n}{i}\left(\dfrac{1 - \sqrt{5}}{2}\right)^i\)
斐波那契通项公式
\(F_n = \dfrac{1}{\sqrt{5}}\left(\left(\dfrac{1 + \sqrt{5}}{2}\right)^n -\left(\dfrac{1 - \sqrt{5}}{2}\right)^n \right)\)
证明
见百度百科
题解:
\(\sum\limits_{i = 0}^{n}\dbinom{n}{i}F_i\)
\(= \sum\limits_{i = 0}^{n}\dbinom{n}{i}\dfrac{1}{\sqrt{5}}\left(\left(\dfrac{1 + \sqrt{5}}{2}\right)^i -\left(\dfrac{1 - \sqrt{5}}{2}\right)^i \right)\)
\(= \dfrac{1}{\sqrt{5}}\sum\limits_{i = 0}^{n}\dbinom{n}{i}\left(\left(\dfrac{1 + \sqrt{5}}{2}\right)^i -\left(\dfrac{1 - \sqrt{5}}{2}\right)^i \right)\)
\(= \dfrac{1}{\sqrt{5}}\sum\limits_{i = 0}^{n}\dbinom{n}{i}\left(\dfrac{1 + \sqrt{5}}{2}\right)^i -\dfrac{1}{\sqrt{5}}\sum\limits_{i = 0}^{n}\dbinom{n}{i}\left(\dfrac{1 - \sqrt{5}}{2}\right)^i\)
这里的变形上面讲过了
\(= \dfrac{1}{\sqrt{5}}\left(1 + \dfrac{1 + \sqrt{5}}{2}\right)^n -\dfrac{1}{\sqrt{5}}\left(1 + \dfrac{1 - \sqrt{5}}{2}\right)^n\)
\(= \dfrac{1}{\sqrt{5}}\left(\dfrac{3 + \sqrt{5}}{2}\right)^n -\dfrac{1}{\sqrt{5}}\left(\dfrac{3 - \sqrt{5}}{2}\right)^n\)
\(= \dfrac{1}{\sqrt{5}}\left(\left(\dfrac{3 + \sqrt{5}}{2}\right)^n -\left(\dfrac{3 - \sqrt{5}}{2}\right)^n \right)\)
\(= \dfrac{1}{\sqrt{5}}\left(\left(\dfrac{6 + 2\sqrt{5}}{4}\right)^n -\left(\dfrac{6 - 2\sqrt{5}}{4}\right)^n \right)\)
\(= \dfrac{1}{\sqrt{5}}\left(\left(\dfrac{1 + 2\sqrt{5} + 5}{4}\right)^n -\left(\dfrac{1 - 2\sqrt{5} + 5}{4}\right)^n \right)\)
\(= \dfrac{1}{\sqrt{5}}\left(\left({\left(\dfrac{1 + \sqrt{5}}{2}\right)^2}\right)^n-\left({\left(\dfrac{1 + \sqrt{5}}{2}\right)^2}\right)^n \right)\)
\(= \dfrac{1}{\sqrt{5}}\left(\left(\dfrac{1 + \sqrt{5}}{2}\right)^{2n} - \left(\dfrac{1 - \sqrt{5}}{2}\right)^{2n} \right)\)
\(= F_{2n}\)
\(Code\)
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#define int long long
#define rr register
#define MAXN 2000010
#define inf 1e9
using namespace std;
const int mod = 1e9 + 7;
inline int read() {
int s = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) {f |= ch == '-'; ch = getchar();}
while (isdigit(ch)) {s = s * 10 + (ch ^ 48); ch = getchar();}
return f ? -s : s;
}
int T, n;
int g[MAXN], inv[MAXN], f[MAXN];
inline void init() {
f[0] = f[1] = 1;
g[0] = 1;
inv[0] = inv[1] = 1;
for (rr int i = 1; i <= 1000000; i++)
g[i] = (i * g[i - 1]) % mod;
for (rr int i = 2; i <= 2000000; i++)
f[i] = (f[i - 1] + f[i - 2]) % mod;
for (rr int i = 2; i <= 1000000; i++)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (rr int i = 2; i <= 1000000; i++)
inv[i] = (inv[i - 1] * inv[i]) % mod;
}
inline int C(int n, int m) {
return (g[n] * inv[m] % mod) * inv[n - m] % mod;
}
signed main() {
init();
T = read();
while (T--) {
n = read();
cout << f[2 * n] << "\n";
}
}