QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#124277#2438. Minimum Spanning TreesHongzyAC ✓697ms3748kbC++174.1kb2023-07-14 16:25:302023-07-14 16:25:31

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-14 16:25:31]
  • 评测
  • 测评结果:AC
  • 用时:697ms
  • 内存:3748kb
  • [2023-07-14 16:25:30]
  • 提交

answer

#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT);
#define rep(i, j, k) for(int i = j; i <= k; ++ i)
#define per(i, j, k) for(int i = j; i >= k; -- i)
using namespace std;

#define fs first
#define sc second
#define pb push_back
#define mp make_pair

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int N = 50;
const int mod = 1e9 + 7;

int qpow(int a, int b) {
  int ans = 1;
  for(; b >= 1; b >>= 1, a = (ll)a * a % mod)
    if(b & 1) ans = (ll)ans * a % mod;
  return ans;
}

namespace ITPL { //O(n^2) Interpolation

vi inter(vi x, vi y) { //in mod
  assert(x.size() == y.size());
  int n = int(x.size()) - 1; //x, y [0, n]
  // rep(i, 0, n) { printf("f(%d) = %d\n", x[i], y[i]); }
  vi p(1, 1);
  rep(i, 0, n) {
    p.pb(0);
    per(j, i + 1, 0) {
      p[j] = ((ll)p[j] * (-x[i]) + (j ? p[j-1] : 0)) % mod;
    }
  }
  vi res(n + 1);
  rep(i, 0, n) {
    vi q(n + 1);
    int z = 1;
    rep(j, 0, n) if(j != i)
      z = (ll)z * (x[i] - x[j]) % mod;
    z = (ll)y[i] * qpow(z, mod-2) % mod;
    q[n] = 1;
    per(j, n, 1)
      q[j-1] = (p[j] + (ll)q[j] * x[i]) % mod;
    rep(j, 0, n)
      res[j] = (res[j] + (ll)z * q[j]) % mod;
  }
  rep(i, 0, n)
    if(res[i] < 0)
      res[i] += mod;
  return res;
}
void Test() {
  //y = 10 x^3 + 5 x^2 - x + 2
  vi v1 = {0, 1, 2, 5}, v2;
  for(int x: v1) v2.pb((10 * x * x * x + 5 * x * x - x + 2 + mod) % mod);
  vi res = inter(v1, v2);
  for(int x: res)
    printf("%d ", x);
  puts("");
}

}

int n, k, p[6], sufp[6];
int f[5][N], c[N][N], g[N][N], fac[N], fav[N], e1[N][N], e0[N][N], pw[5 * N];
void upd(int &x, ll y) {
  x = (x + y) % mod;
}
int solve(int x0) {
  pw[0] = 1;
  rep(i, 1, (k-1) * n)
    pw[i] = (ll)pw[i-1] * x0 % mod;
  f[0][1] = 1;
  rep(t, 1, k) {
    rep(i, 0, n)
      rep(j, 1, n - i) {
        int c = i * j;
        e0[i][j] = qpow((sufp[t+1] + p[0]) % mod, c);
        e1[i][j] = (qpow((p[0] + sufp[t]) % mod, c) - e0[i][j]) % mod;
        // printf("e%d %d = %d %d\n", i, j, e0[i][j], e1[i][j]);
      }

    rep(s, 1, n)
      rep(i, 0, n-s)
        g[s][i] = !i;
    rep(d, 1, n) {
      // printf("d = %d\n", d);
      f[t][d] = 0;
      rep(i, 1, d)
        upd(f[t][d], (ll)f[t-1][i] * g[i][d-i] % mod * c[d-1][i-1]);
      per(i, n, d) {//g[s][i]
        rep(s, 1, n-i) {
          int t1 = 1, _t3 = 1, t3 = 1;
          rep(k, 1, i / d) {
            // printf("d = %d, i = %d, k = %d\n",  d, i, k);
            // assert(f[t][d]);
            t1 = (ll)t1 * fav[d] % mod * e1[s][d] % mod * f[t][d] % mod; //d < z
            t3 = (ll)t3 * _t3 % mod; //pow(e0[d][d], k*(k-1)/2 = 0 + 1 + .. + k-1)
            _t3 = (ll)_t3 * e0[d][d] % mod; //pow(e0[d][d], k)
            //f: d < s+i
            // ll tt = (ll)g[s][i - k*d] * pw[(t-1)*k] % mod
              // * fac[i] % mod * fav[k] % mod * fav[i-k*d] % mod * e0[i - k*d][k*d] % mod * t1 % mod * t3;
            // cout << ((tt+mod) % mod) << endl;
            upd(g[s][i], (ll)g[s][i - k*d] * pw[(t-1)*k] % mod
              * fac[i] % mod * fav[k] % mod * fav[i-k*d] % mod * e0[i - k*d][k*d] % mod * t1 % mod * t3);
          }
        }
      }
      
      // rep(i, 1, n-s)
        // printf("g%d %d = %d\n", s, i, (g[s][i] + mod)%mod);
    }
  }
  return f[k][n];
}


int main() {

  rep(i, 0, N-1) {
    c[i][0] = 1;
    rep(j, 1, i)
      c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod;
  }
  *fac = 1;
  rep(i, 1, N-1) fac[i] = (ll)fac[i-1] * i % mod;
  fav[N-1] = qpow(fac[N-1], mod-2);
  per(i, N-1, 1) fav[i-1] = (ll)fav[i] * i % mod;
  // Test();
  const int i100 = qpow(100, mod-2);
  int test;
  scanf("%d", &test);
  rep(T, 1, test) {
    scanf("%d%d", &n, &k);
    rep(i, 0, k) scanf("%d", p + i), p[i] = (ll)p[i] * i100 % mod;
    sufp[k+1] = 0;
    per(i, k, 0) sufp[i] = (sufp[i+1] + p[i]) % mod;
    vi x((k - 1) * (n - 1) + 1);
    iota(x.begin(), x.end(), 0);
    vi y;
    for(int x0: x) y.pb(solve(x0));
    
    vi res = ITPL::inter(x, y);
    int o = res.size();
    for(int z: res) {
      printf("%d%c", z, " \n"[!--o]);
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 697ms
memory: 3748kb

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