QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#462789 | #2438. Minimum Spanning Trees | hos_lyric | AC ✓ | 27ms | 3940kb | C++14 | 7.4kb | 2024-07-04 04:15:57 | 2024-07-04 04:15:58 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;
constexpr int LIM_INV = 1010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
void prepare() {
inv[1] = 1;
for (int i = 2; i < LIM_INV; ++i) {
inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
}
fac[0] = invFac[0] = 1;
for (int i = 1; i < LIM_INV; ++i) {
fac[i] = fac[i - 1] * i;
invFac[i] = invFac[i - 1] * inv[i];
}
}
Mint binom(Int n, Int k) {
if (n < 0) {
if (k >= 0) {
return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
} else if (n - k >= 0) {
return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
} else {
return 0;
}
} else {
if (0 <= k && k <= n) {
assert(n < LIM_INV);
return fac[n] * invFac[k] * invFac[n - k];
} else {
return 0;
}
}
}
using Poly = vector<Mint>;
void operator<<=(Poly &as, int k) {
as.insert(as.begin(), k, 0);
}
void operator>>=(Poly &as, int k) {
as.erase(as.begin(), as.begin() + min(k, (int)as.size()));
}
void operator*=(Poly &as, Mint t) {
for (Mint &a : as) a *= t;
}
void operator+=(Poly &as, const Poly &bs) {
if (as.size() < bs.size()) as.resize(bs.size(), 0);
for (int i = 0; i < (int)bs.size(); ++i) as[i] += bs[i];
}
void operator-=(Poly &as, const Poly &bs) {
if (as.size() < bs.size()) as.resize(bs.size(), 0);
for (int i = 0; i < (int)bs.size(); ++i) as[i] -= bs[i];
}
Poly operator*(const Poly &as, const Poly &bs) {
const int asLen = as.size();
const int bsLen = bs.size();
if (asLen == 0 || bsLen == 0) return Poly{};
Poly cs(asLen + bsLen - 1, 0);
for (int i = 0; i < asLen; ++i) for (int j = 0; j < bsLen; ++j) cs[i + j] += as[i] * bs[j];
return cs;
}
void Exp(vector<Poly> &F) {
const int len = F.size();
for (int i = 0; i < len; ++i) F[i] *= i;
vector<Poly> G(len);
G[0] = Poly{1};
for (int i = 1; i < len; ++i) {
for (int j = 1; j <= i; ++j) G[i] += F[j] * G[i - j];
G[i] *= inv[i];
}
F.swap(G);
}
void Log(vector<Poly> &F) {
const int len = F.size();
auto G = F;
for (int i = 1; i < len; ++i) {
G[i] *= i;
for (int j = 1; j < i; ++j) G[i] -= F[i - j] * G[j];
}
for (int i = 1; i < len; ++i) G[i] *= inv[i];
F.swap(G);
}
/*
F[k] := \sum[n,m] Pr[complete graph on [n], connected by weight <= k, MST = m] x^n/n! y^m
double counting:
- connect some F[k-1]'s by weight >= k
- each component costs y^k
- connect some F[k]'s by weight > k
*/
int N, K;
vector<Mint> P;
int main() {
prepare();
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &K);
P.resize(K + 1);
for (int k = 0; k <= K; ++k) {
scanf("%u", &P[k].x);
P[k] /= 100;
}
vector<Poly> F(N + 1);
F[1] = Poly{1};
for (int k = 1; k <= K; ++k) {
Mint q = P[0];
for (int l = k + 1; l <= K; ++l) q += P[l];
const Mint p = P[k] + q;
if (p) {
for (int n = 0; n <= N; ++n) F[n] <<= k;
for (int n = 0; n <= N; ++n) F[n] *= p.pow(-n*(n-1)/2);
Exp(F);
for (int n = 0; n <= N; ++n) F[n] *= p.pow(n*(n-1)/2);
if (q) {
for (int n = 0; n <= N; ++n) F[n] *= q.pow(-n*(n-1)/2);
Log(F);
for (int n = 0; n <= N; ++n) F[n] *= q.pow(n*(n-1)/2);
}
for (int n = 0; n <= N; ++n) F[n] >>= k;
}
// cerr<<"k = "<<k<<endl;for(int n=0;n<=N;++n)cerr<<"F["<<n<<"] = "<<F[n]<<endl;
}
Poly ans = F[N];
ans *= fac[N];
ans.resize(K * (N-1) + 1, 0);
for (int m = N-1; m <= K * (N-1); ++m) {
if (m > N-1) printf(" ");
printf("%u", ans[m].x);
}
puts("");
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 27ms
memory: 3940kb
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 20527108 7248...
result:
ok 200 lines