交错序列
题目背景:
分析:DP + 矩阵快速幂
第一反应看到数据范围就觉得,可以直接O(n) ~ O(n)搞过去,但是很遗憾常数过大,至少我没有卡过去,换种思路考虑化简一下。
那么我们只需要求得所有方案中0的个数的i次方之和(0 <= i <= a + b),那么我们定义f[i][j][0/1]表示前i位的所有合法方案的0的个数的j次方之和,第i位是0/1,那么显然对于一个f[i][j][1]直接等于f[i - 1][j][0],对于f[i][j][0]相当于把所有的方案中的xi变成(x + 1)i这个可以直接二项式定理获得对应系数,即:
利用矩阵优化即可,注意适当剪枝,复杂度O((a + b)3 * logn)
Source:
/* created by scarlyw */ #include <cstdio> #include <string> #include <algorithm> #include <cstring> #include <iostream> #include <cmath> #include <cctype> #include <vector> #include <set> #include <queue> inline char read() { static const int IN_LEN = 1024 * 1024; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; } ///* template<class T> inline void R(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return ; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0'); if (iosig) x = -x; } //*/ const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN], *oh = obuf; inline void write_char(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template<class T> inline void W(T x) { static int buf[30], cnt; if (x == 0) write_char('0'); else { if (x < 0) write_char('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) write_char(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } /* template<class T> inline void R(T &x) { static char c; static bool iosig; for (c = getchar(), iosig = false; !isdigit(c); c = getchar()) if (c == '-') iosig = true; for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0'); if (iosig) x = -x; } //*/ const int MAXN = 180 + 10; int n, a, b, mod; long long fac[MAXN], inv_fac[MAXN]; inline long long mod_pow(long long a, long long b) { long long ans = 1; for (; b; b >>= 1, a = a * a % mod) if (b & 1) ans = ans * a % mod; return ans; } inline long long c(int n, int m) { return (n < m) ? (0) : (fac[n] * inv_fac[m] % mod * inv_fac[n - m] % mod); } struct matrix { int n; long long a[MAXN][MAXN]; matrix() {} matrix(int n) : n(n) { for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) a[i][j] = 0; } inline matrix operator * (const matrix &c) const { matrix ans = matrix(n); for (int i = 0; i < n; ++i) for (int k = 0; k < n; ++k) if (a[i][k]) for (int j = 0; j < n; ++j) if (c.a[k][j]) ans.a[i][j] = (ans.a[i][j] + a[i][k] * c.a[k][j]) % mod; return ans; } inline matrix operator ^ (const int x) const { matrix a = *this, ans = matrix(n); for (int i = 0; i < n; ++i) ans.a[i][i] = 1; int b = x; for (; b; b >>= 1, a = a * a) if (b & 1) ans = ans * a; return ans; } } move; inline void solve() { R(n), R(a), R(b), R(mod); int max = a + b + 1; fac[0] = 1; for (int i = 1; i <= max; ++i) fac[i] = fac[i - 1] * i % mod; inv_fac[max] = mod_pow(fac[max], mod - 2); for (int i = max - 1; i >= 0; --i) inv_fac[i] = inv_fac[i + 1] * (i + 1) % mod; move = matrix(2 * max); for (int i = 0; i < max; ++i) move.a[i][i + max] = 1; for (int i = 0; i < max; ++i) for (int j = 0; j <= i; ++j) move.a[j][i] = move.a[j + max][i] = c(i, j); move = (move ^ n); long long x = 1, ans = 0; for (int i = 0; i <= b; ++i, x = x * n % mod) { ans += c(b, i) * x % mod * (move.a[0][a + b - i] + move.a[0][a + b - i + max]) % mod * ((b - i & 1) ? (-1) : (1)); ans %= mod; } std::cout << (ans + mod) % mod; } int main() { //freopen("in.in", "r", stdin); solve(); return 0; }