P2012 拯救世界2
三种基因,我们分别列出其生成函数:
F(x)=∑n≥0xnn!=exG(x)=∑n≥0x2n+1(2n+1)!=12(∑n≥0xnn!−∑n≥0(−1)nxnn!)=12(ex−e−x)H(x)=∑n≥0x2n(2n)!=12(∑n≥0xnn!+∑n≥0(−1)nxnn!)=12(ex+e−x)F(x) = \sum_{n \geq 0} \frac{x ^ n}{n!} = e ^ x\\ G(x) = \sum_{n \geq 0} \frac{x ^{2n + 1}}{(2n + 1)!} = \frac{1}{2}\left(\sum_{n \geq 0} \frac{x ^ n}{n!} - \sum_{n \geq 0} (-1) ^ n \frac{x ^ n}{n !} \right) = \frac{1}{2} \left(e ^ x - e ^ {-x}\right)\\ H(x) = \sum_{n \geq 0} \frac{x ^{2n}}{(2n)!} = \frac{1}{2}\left(\sum_{n \geq 0} \frac{x ^ n}{n!} + \sum_{n \geq 0} (-1) ^ n \frac{x ^ n}{n !} \right) = \frac{1}{2}\left(e ^ x + e ^{-x}\right)\\ F(x)=n≥0∑n!xn=exG(x)=n≥0∑(2n+1)!x2n+1=21(n≥0∑n!xn−n≥0∑(−1)nn!xn)=21(ex−e−x)H(x)=n≥0∑(2n)!x2n=21(n≥0∑n!xn+n≥0∑(−1)nn!xn)=21(ex+e−x)
A(x)=(F(x)G(x)H(x))4=1256(e12x−4e8x+64x+e−4x−4)对上面进行泰勒展开得到第n项的系数再乘上n!就是答案了ans[n]=1256(12n−4×8n+6×4n+(−4)n)A(x) = \left(F(x)G(x)H(x)\right) ^ 4 = \frac{1}{256}\left(e ^{12x} - 4e^{8x} + 6^{4x} + e ^{-4x} - 4\right) \\ 对上面进行泰勒展开得到第n项的系数再乘上n!就是答案了\\ ans[n] = \frac{1}{256}\left(12 ^ n - 4 \times 8 ^ n + 6 \times 4 ^n + (-4) ^ n\right)\\ A(x)=(F(x)G(x)H(x))4=2561(e12x−4e8x+64x+e−4x−4)对上面进行泰勒展开得到第n项的系数再乘上n!就是答案了ans[n]=2561(12n−4×8n+6×4n+(−4)n)
#include <bits/stdc++.h>using namespace std;const int mod = 1e9, phi = 1e8;int quick_pow(int a, int n) {int ans = 1;while (n) {if (n & 1) {ans = 1ll * ans * a % mod;}a = 1ll * a * a % mod;n >>= 1;}return ans;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);long long n;while (scanf("%lld", &n) && n) {if (n < 4) {puts("0");}else if (n < phi) {printf("%lld\n", (1ll * 81 * quick_pow(12, n - 4) % mod - quick_pow(8, n - 2) + 1ll * 6 * quick_pow(4, n - 4) + quick_pow(-4, n - 4) + mod) % mod);}else {int ans = 1ll * 81 * quick_pow(12, (n - 4) % phi + phi) % mod;ans = (ans - quick_pow(8, (n - 2) % phi + phi) + mod) % mod;ans = (ans + 1ll * 6 * quick_pow(4, (n - 4) % phi + phi) % mod) % mod;int res = quick_pow(4,(n - 4) % phi + phi);if((n - 4) & 1){ans = (ans - res + mod) % mod;}else {ans = (ans + res) % mod;}printf("%d\n", ans);}}return 0;
}