QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#69603#2438. Minimum Spanning Treeshe_ren_shi_lyp#AC ✓21ms3668kbC++235.3kb2022-12-28 22:14:092022-12-28 22:14:11

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-28 22:14:11]
  • 评测
  • 测评结果:AC
  • 用时:21ms
  • 内存:3668kb
  • [2022-12-28 22:14:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int mod = 1e9 + 7;
constexpr int N = 45, K = 6;
int n, k, m, p[K], q[K];
int power[N * K][K], invpower[N * K][K];
int inv[N * K], invfac[N * K], fac[N * K];
void upd(int &x, int y) {x += y - mod, x += x >> 31 & mod;}
int add(int x, int y) {	upd(x, y); return x;}
int fpw(int a, int b) {
	int ans = 1;
	for(; b; b >>= 1, a = 1ll * a * a % mod) if(b & 1) ans = 1ll * ans * a % mod;
	return ans;
}
struct poly {
	int a[N * K];
	poly() {
		memset(a, 0, sizeof(a));
	}
	poly(int x) {
		fill(a + 1, a + m + 2, 1);
	}
	int& operator [] (int x) {return a[x];}
	int operator[] (int x) const {return a[x];}
	friend poly operator + (const poly &x, const poly &y) {
		poly z;
		for(int i = 1; i <= m + 1; i ++) z[i] = add(x[i], y[i]);
		return z;
	}
	friend poly operator - (const poly &x, const poly &y) {
		poly z;
		for(int i = 1; i <= m + 1; i ++) z[i] = add(x[i], mod - y[i]);
		return z;
	}
	friend poly operator * (const poly &x, const poly &y) {
		poly z;
		for(int i = 1; i <= m + 1; i ++) z[i] = 1ll * x[i] * y[i] % mod;
		return z;
	}
	friend poly operator * (const poly &x, const int &y) {
		poly z;
		for(int i = 1; i <= m + 1; i ++) z[i] = 1ll * x[i] * y % mod;
		return z;
	}
	friend poly operator / (const poly &x, const int &y) {
		poly z; int invy = fpw(y, mod - 2);
		for(int i = 1; i <= m + 1; i ++) z[i] = 1ll * x[i] * invy % mod;
		return z;
	}
	friend poly operator << (const poly &x, const int &y) {
		poly z;
		for(int i = 1; i <= m + 1; i ++) z[i] = 1ll * x[i] * power[i][y] % mod;
		return z;
	}
	friend poly operator >> (const poly &x, const int &y) {
		poly z;
		for(int i = 1; i <= m + 1; i ++) z[i] = 1ll * x[i] * invpower[i][y] % mod;
		return z;
	}
	poly& operator += (const poly &y) {
		return *this = *this + y;
	}
	poly& operator -= (const poly &y) {
		return *this = *this - y;
	}
	poly& operator *= (const poly &y) {
		return *this = *this * y;
	}
	poly& operator *= (const int &y) {
		return *this = *this * y;
	}
	poly& operator /= (const int &y) {
		return *this = *this / y;
	}
	poly& operator <<= (const int &y) {
		return *this = *this << y;
	}
	poly& operator >>= (const int &y) {
		return *this = *this >> y;
	}
	auto lar() {
		vector<int> all(m + 2), ans(m + 1);
		all[0] = 1;
		for(int i = 1; i <= m + 1; i ++) {
			for(int j = i; j >= 1; j --) all[j] = add(mod - 1ll * all[j] * i % mod, all[j - 1]);
			all[0] = mod - 1ll * all[0] * i % mod;
		}
		for(int i = 1; i <= m + 1; i ++) {
			vector<int> res = all;
			int invi = invpower[i][1], inv = 1ll * invfac[i - 1] * invfac[m + 1 - i] % mod;
			if(m - i + 1 & 1) inv = mod - inv;
			for(int j = 0; j <= m; j ++) {
				int tmp = mod - 1ll * res[j] * invi % mod;
				res[j] = tmp;
				res[j + 1] = add(res[j + 1], mod - tmp);
			}
			assert(res[m + 1] == 0);
			for(int j = 0; j <= m; j ++) {
				ans[j] = add(ans[j], 1ll * res[j] * a[i] % mod * inv % mod);
			}
		}
		return ans;
	}
	friend ostream& operator << (ostream &out, poly x) {
		auto vec = x.lar();
		for(int i = 0; i <= m; i ++) out << vec[i] << ' ';
		return out;
	}
};
poly f[K][N];
void init() {
	inv[1] = invfac[0] = fac[0] = 1;
	for(int i = 2; i <= m + 1; i ++) inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod;
	for(int i = 1; i <= m + 1; i ++) invfac[i] = 1ll * invfac[i - 1] * inv[i] % mod;
	for(int i = 1; i <= m + 1; i ++) fac[i] = 1ll * fac[i - 1] * i % mod;
	for(int i = 1; i <= m + 1; i ++) {
		power[i][0] = 1;
		invpower[i][0] = 1;
		for(int j = 1; j <= k; j ++) {
			power[i][j] = 1ll * power[i][j - 1] * i % mod;
			invpower[i][j] = 1ll * invpower[i][j - 1] * inv[i] % mod;
		}
	}
}
void exp(poly *f, poly *g) {
	for(int i = 1; i <= n; i ++) g[i] = poly();
	g[0] = poly(1);
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j <= i; j ++) {
			g[i] += f[j] * j * g[i - j];
		}
		g[i] /= i;
	}
}
void ln(poly *f, poly *g) {
	for(int i = 0; i <= n; i ++) g[i] = poly();
	for(int i = 0; i <= n; i ++) {
		for(int j = 1; j < i; j ++) {
			g[i] += g[j] * j  * f[i - j];
		}
		g[i] /= i;
		g[i] = f[i] - g[i];
	}
}
int C2(int x) {return 1ll * x * (x - 1) / 2;}
int main() {
	// freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0);
	int T;
	for(cin >> T; T; T --) {
		cin >> n >> k; m = n * k;
		init();
		{
			int inv = fpw(100, mod - 2);
			for(int i = 0; i <= k; i ++) cin >> p[i], p[i] = 1ll * p[i] * inv % mod;
		}
		q[k + 1] = p[0];
		for(int i = k; i >= 1; i --) {
			q[i] = add(q[i + 1], p[i]);
		} 
		f[0][1] = poly(1);
		for(int c = 1; c <= k; c ++) {
			for(int i = 1; i <= n; i ++) {
				f[c - 1][i] /= fpw(q[c], C2(i));
				f[c - 1][i] /= fac[i];
				f[c - 1][i] <<= c;
			}
			static poly tmp[N];
			exp(f[c - 1], tmp);
			for(int i = 1; i <= n; i ++) {
				tmp[i] *= fpw(q[c], C2(i));
			}
			if(q[c + 1]) {
				for(int i = 1; i <= n; i ++) tmp[i] /= fpw(q[c + 1], C2(i));
				ln(tmp, f[c]);
				for(int i = 1; i <= n; i ++) {
					f[c][i] >>= c;
					f[c][i] *= fac[i];
					f[c][i] *= fpw(q[c + 1], C2(i));
				}
			} else {
				for(int i = 1; i <= n; i ++) f[c][i] = (tmp[i] >> c) * fac[i];
				break;
			}
		}
		auto ans = f[k][n].lar();
		for(int i = n - 1; i <= (n - 1) * k; i ++) cout << ans[i] << ' '; cout << '\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 21ms
memory: 3668kb

input:

200
3 1
50 50
3 2
0 50 50
3 3
25 25 25 25
8 1
41 59
7 3
37 30 7 26
3 3
16 12 18 54
9 2
9 43 48
9 3
3 40 42 15
9 1
29 71
9 2
40 42 18
5 1
76 24
5 1
39 61
9 2
23 38 39
10 4
18 15 34 2 31
7 2
23 28 49
9 4
15 13 25 19 28
7 1
64 36
6 1
50 50
9 1
4 96
4 1
64 36
9 2
24 45 31
9 2
3 61 36
9 1
65 35
8 4
6 1 3...

output:

500000004 
500000004 375000003 125000001 
406250003 109375001 250000002 265625002 562500004 
858129220 
40267248 73443306 307645653 13908396 542571454 781149891 223877799 478284083 469782292 483514097 271207900 851118600 686534546 
708608005 271088002 536992004 107032001 243224002 
763536836 2052710...

result:

ok 200 lines