QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#250093#5244. Drzewa rozpinające [A]hos_lyric0 0ms4068kbC++146.6kb2023-11-12 21:20:362023-11-12 21:20:37

Judging History

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

  • [2023-11-12 21:20:37]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:4068kb
  • [2023-11-12 21:20:36]
  • 提交

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>;

vector<Mint> findLinearRecurrence(const vector<Mint> &as) {
  const int n = as.size();
  int d = 0, m = 0;
  vector<Mint> cs(n + 1, 0), bs(n + 1, 0);
  cs[0] = bs[0] = 1;
  Mint invBef = 1;
  for (int i = 0; i < n; ++i) {
    ++m;
    Mint dif = as[i];
    for (int j = 1; j < d + 1; ++j) dif += cs[j] * as[i - j];
    if (dif.x != 0) {
      auto csDup = cs;
      const Mint r = dif * invBef;
      for (int j = m; j < n; ++j) cs[j] -= r * bs[j - m];
      if (2 * d <= i) {
        d = i + 1 - d;
        m = 0;
        bs = csDup;
        invBef = dif.inv();
      }
    }
  }
  cs.resize(d + 1);
  return cs;
}

unsigned xrand() {
  static unsigned x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  unsigned t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}

template <class F> Mint det(int n, F f) {
  for (; ; ) {
    vector<Mint> xs(n), ys(n);
    for (int i = 0; i < n; ++i) xs[i] = xrand();
    for (int i = 0; i < n; ++i) ys[i] = xrand();
    vector<Mint> as(2 * n, 0);
    for (int i = 0; i < 2 * n; ++i) {
      for (int j = 0; j < n; ++j) as[i] += xs[j] * ys[j];
      ys = f(ys);
    }
    const auto cs = findLinearRecurrence(as);
    if ((int)cs.size() == n + 1) return ((n&1)?-1:+1) * cs[n];
  }
}


int N;
vector<int> A;

int M;
vector<int> lpf, phi;

/*
  \sum[v] gcd(A[u], A[v]) ys[v]
  = \sum[v] (\sum[i] [i | A[u]] [i | A[v]] phi(i)) ys[v]
  = \sum[i] [i | A[u]] phi(i) \sum[v] [i | A[v]] ys[v]
*/
vector<Mint> calc(const vector<Mint> &ys) {
  const int ysLen = ys.size();
  vector<Mint> fs(M + 1, 0);
  for (int v = 0; v < ysLen; ++v) fs[A[v]] += ys[v];
  for (int p = 2; p <= M; ++p) if (lpf[p] == p) {
    for (int n = M / p, nn = n * p; n; --n, nn -= p) fs[n] += fs[nn];
  }
  for (int n = 1; n <= M; ++n) fs[n] *= phi[n];
  for (int p = 2; p <= M; ++p) if (lpf[p] == p) {
    for (int n = 1, nn = p; nn <= M; ++n, nn += p) fs[nn] += fs[n];
  }
  vector<Mint> ret(ysLen);
  for (int u = 0; u < ysLen; ++u) ret[u] = fs[A[u]];
  return ret;
}

int main() {
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%d", &A[u]);
    }
    
    M = *max_element(A.begin(), A.end());
    lpf.assign(M + 1, 0);
    phi.assign(M + 1, 0);
    for (int p = 2; p <= M; ++p) lpf[p] = p;
    for (int n = 1; n <= M; ++n) phi[n] = n;
    for (int p = 2; p <= M; ++p) if (lpf[p] == p) {
      for (int n = p; n <= M; n += p) {
        chmin(lpf[n], p);
        (phi[n] /= p) *= (p - 1);
      }
    }
    
    const auto deg = calc(vector<Mint>(N, 1));
// cerr<<"deg = "<<deg<<endl;
    const Mint ans = det(N - 1, [&](const vector<Mint> &ys) -> vector<Mint> {
      auto res = calc(ys);
      for (int u = 0; u < N - 1; ++u) res[u] = deg[u] * ys[u] - res[u];
      return res;
    });
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 1
Accepted
time: 0ms
memory: 3808kb

input:

1
3

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

2
4 2

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3820kb

input:

3
2 7 5

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3812kb

input:

4
2 5 5 2

output:

72

result:

ok single line: '72'

Test #5:

score: 0
Accepted
time: 0ms
memory: 4068kb

input:

5
1 8 4 8 4

output:

7225

result:

ok single line: '7225'

Test #6:

score: -1
Time Limit Exceeded

input:

6
1 6 1 3 4 5

output:


result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #10:

score: 0
Time Limit Exceeded

input:

500
388 351 42 430 179 19 288 101 447 415 133 236 109 312 15 333 348 119 81 264 67 262 426 279 145 397 210 300 212 40 164 24 373 63 301 482 115 344 60 230 295 456 51 363 224 327 338 110 97 458 274 96 46 135 50 411 499 59 352 191 372 211 91 483 357 15 348 284 94 96 367 281 100 403 212 350 8 111 32 33...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #19:

score: 0
Time Limit Exceeded

input:

1000
610 997 196 509 978 104 682 466 660 969 406 839 987 15 916 440 364 420 29 997 685 944 943 75 64 51 838 481 779 833 269 678 447 948 626 293 220 961 529 868 337 492 13 953 207 949 845 537 910 694 212 425 14 682 203 351 99 970 567 530 785 779 331 312 494 961 66 60 912 881 409 291 652 740 757 936 8...

output:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #28:

score: 0
Time Limit Exceeded

input:

3200
1539 1480 1859 627 831 1495 1865 2906 1303 1232 1167 206 489 1045 2498 693 2846 2137 3184 1200 1396 1065 1418 2769 2659 1054 600 2248 559 2974 2691 241 2693 819 970 493 1360 1579 2599 2158 326 869 1370 356 415 885 899 2826 1235 2958 555 1150 2190 1952 502 1390 701 1141 2678 2669 769 2917 2439 4...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #37:

score: 0
Time Limit Exceeded

input:

3500
874 1486 3240 2876 2120 552 2467 665 880 365 1254 2025 1339 197 688 3131 3384 3317 2145 1045 2117 2529 2428 2795 3078 2577 1872 2668 502 975 544 390 1499 2288 1721 1930 19 2098 3242 1350 182 2448 679 2359 944 1016 979 3059 2288 1169 179 166 144 1577 2956 270 2157 2698 2438 650 3274 1057 2515 21...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #46:

score: 0
Time Limit Exceeded

input:

3800
2315 2518 662 2648 1371 1971 2374 499 294 2460 1071 230 503 206 140 766 3094 3071 3370 1300 3118 1209 3666 972 2220 1827 440 45 674 865 1929 1207 2824 1213 350 1786 2647 702 3616 788 1616 3463 2524 794 786 234 774 319 1810 2563 3469 1134 3665 1316 684 3587 3604 1256 1060 3119 1290 3200 1330 128...

output:


result:


Subtask #7:

score: 0
Time Limit Exceeded

Test #55:

score: 0
Time Limit Exceeded

input:

4100
686 3769 3413 2407 3553 855 3799 2773 3379 1826 3852 1087 1268 247 3521 3935 463 219 1771 2172 1210 2889 2074 867 3403 3107 2392 1502 1346 911 612 1643 1552 2626 885 2903 3218 4055 1013 1641 2892 2100 291 2173 1634 992 1187 3539 2193 1168 3716 1310 1698 3830 1958 2061 2995 909 2913 1805 810 912...

output:


result:


Subtask #8:

score: 0
Time Limit Exceeded

Test #64:

score: 0
Time Limit Exceeded

input:

4400
260 3770 2637 3654 540 2 3775 1444 1838 318 2500 1146 4325 1214 856 4283 4396 3031 299 330 3431 3019 452 1202 3265 3081 2550 3860 4137 4173 1766 1912 1487 3842 4163 3009 495 3503 3649 266 746 1096 836 394 225 2590 1039 1804 2025 2278 2519 2979 671 296 1704 4025 4113 2342 1192 4279 2234 3166 37 ...

output:


result:


Subtask #9:

score: 0
Time Limit Exceeded

Test #73:

score: 0
Time Limit Exceeded

input:

4700
3056 4696 2025 918 614 2826 1716 4088 2833 3985 811 2428 1498 2827 1408 2121 4324 818 3281 4289 3931 1842 3071 4502 1410 3176 2098 3633 987 2942 3273 3338 3373 1868 976 4586 3537 2499 2 4477 383 560 2164 4542 4207 4279 586 327 3905 2363 4213 4460 1123 1314 763 1451 1437 4639 1268 4392 1516 4086...

output:


result:


Subtask #10:

score: 0
Time Limit Exceeded

Test #82:

score: 0
Time Limit Exceeded

input:

5000
4147 226 2474 4662 2345 2615 1968 2849 422 784 2041 3683 4926 850 3089 2033 2149 4446 2279 836 3286 1170 2724 2804 1745 2310 234 90 2671 3299 2733 1543 3544 3330 3473 31 2648 34 4607 4948 3106 1136 977 76 2899 4245 966 51 2567 2609 3166 2195 3273 2450 5000 4147 955 1011 2346 800 4413 3556 1118 ...

output:


result: