简单粗暴的题目
题目背景:
thoj25
分析:本题一看真的很粗暴,在一想也的确非常粗爆,那么我们就用粗暴的方法,首先,我们发现直接暴力,我们需要算n2个数的k次方,显然复杂度上是不能接受的,我们发现对于第i个answer其实就是,S(i)k + (S(i)+ S(i - 1))k + (S(i) + S(i - 1) + S(i - 2))k + ... + (S(i)+ S(i - 1) +...+ S(1))k,
我们考虑对于S进行一个求和,我们令sum(i) = S(1) + S(2) +... + S(i),那么针对每一个等式,我们所求的就应该是ans(i) = (sum(i))k+ (sum(i) – sum(1))k + (sum(i) – sum(2))k + (sum(i) – sum(3))k+...+ (sum(i) – sum(i - 1))k, 那我们用二项式定理将其展开后就发现,我们可以通过暴力求的sum(i)的0 ~ k次方,预处理出组合数,然后每一次将一个sum累加进数组,然后和下一个sum进行计算,因为系数相同完全可以将每一项的i次方合并在一起。
Source:
#include #include #include #include #include #include #include using namespace std; 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 inline bool R(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return false; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = (x << 3) + (x << 1) + (c ^ '0'); if (iosig) x = -x; return true; } const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN], *oh = obuf; inline void writechar(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template inline void W(T x) { static int buf[30], cnt; if (!x) writechar(48); else { if (x < 0) writechar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writechar(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } const int mod = 1e9 + 7; const int MAXN = 50000 + 10; const int MAXK = 100 + 10; int n, k; int s[MAXN], val[MAXK], cur[MAXK], c[MAXK][MAXK]; long long ans; inline void readin() { R(n), R(k); for (int i = 1; i <= n; ++i) R(s[i]); } inline void pre(int k) { for (int i = 0; i <= k; ++i) c[0][i] = c[i][i] = 1; for (int i = 1; i <= k; ++i) /*预处理组合数*/ for (int j = 1; j <= i - 1; ++j) c[j][i] = (c[j][i - 1] + c[j - 1][i - 1]) % mod; } template inline void add(T &x, int t) { x += t; if (x > mod) x -= mod; if (x < 0) x += mod; } inline void work() { for (int i = 1; i <= n; ++i) add(s[i], s[i - 1]); /*得到sum数组*/ for (int i = 1; i <= n; ++i) { cur[0] = 1; for (int j = 1; j <= k; ++j) cur[j] = 1LL * cur[j - 1] * s[i] % mod; /*暴力计算当前sum的k次方*/ ans = cur[k]; for (int j = 0; j <= k; ++j) add(ans, 1LL * c[j][k] * cur[k - j] % mod * val[j] * ((j & 1) ? -1 : 1) % mod); /*合并计算当前的答案*/ W(ans), writechar(' '); for (int j = 0; j <= k; ++j) add(val[j], cur[j]); /*将当前的i次方累加进之前所求的全部*/ } } int main() { // freopen("in.in", "r", stdin); readin(); pre(k + 5); work(); flush(); return 0; }