Submission #31087608


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x) \
{ \
  std::cout << PREFIX << #x << " = " << x << std::endl; \
}
std::ostream& output_indent(std::ostream& os, int ind) {
  for(int i = 0; i < ind; i++) os << " ";
  return os;
}
template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p);
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
  os << "[";
  for(int i = 0;i < v.size();i++) os << v[i] << ", ";
  return (os << "]");
}
template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); }
template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}

#include <iostream>
using i64 = long long;
template<i64 M> struct modint { i64 a;
  constexpr modint(const i64 x = 0): a((x%M+M)%M){}
  constexpr i64 value() const { return a; }
  constexpr modint inv() const { return this->pow(M-2); }
  constexpr modint pow(i64 r) const {
    modint ans(1); modint aa = *this;
    while(r) { if(r & 1) ans *= aa; aa *= aa; r >>= 1; }
    return ans;
  }
  constexpr bool operator==(const modint& r) const { return a == r.a; }
  constexpr bool operator!=(const modint& r) const { return a != r.a; }
  constexpr modint& operator=(const i64 r) { a = (r % M + M) % M; return *this; }
  constexpr modint& operator+=(const modint r) { a += r.a; if(a >= M) a -= M; return *this; }
  constexpr modint& operator-=(const modint r) { a -= r.a; if(a < 0) a += M; return *this; }
  constexpr modint& operator*=(const modint r) { a = a * r.a % M; return *this; }
  constexpr modint& operator/=(const modint r) { (*this) *= r.inv(); return *this; }
  constexpr modint operator+(const modint r) const { return modint(*this) += r; }
  constexpr modint operator-(const modint r) const { return modint(*this) -= r; }
  constexpr modint operator-() const { return modint(0) - modint(*this); }
  constexpr modint operator*(const modint r) const { return modint(*this) *= r; }
  constexpr modint operator/(const modint r) const { return modint(*this) /= r; }
  constexpr bool operator!=(const modint r) const { return this->value() != r.value(); }
};

template<const i64 M> std::ostream& operator<<(std::ostream& os, const modint<M>& m) { os << m.value(); return os; }


using fp = modint<998244353>;

i64 N;
vector<i64> A;
vector<vector<int>> G;
vector<vector<int>> dv(1e5 + 1);

vector<int> vis;

void dfs(int v, int p, int g, vector<i64>& cnt) {
  vis[v] = 1;
  i64 now = 1;
  for(auto u: G[v]) {
    if(u == p) continue;
    if(A[u] % g != 0) continue;
    dfs(u, v, g, cnt);
    now += cnt.back();
  }
  cnt.push_back(now);
}

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template <class T>
void divisor_transform(vector<T> &a) {
    int n = a.size();
    vector<bool> sieve(n, true);
    for (int p = 2; p < n; ++p) {
        if (sieve[p]) {
            for (int k = 1; k * p < n; ++k) {
                sieve[k * p] = false;
                a[k * p] += a[k];
            }
        }
    }
    for (int i = 0; ++i != n;) {
        a[i] += a[0];
    }
}

template <class T>
void inverse_divisor_transform(vector<T> &a) {
    int n = a.size();
    vector<bool> sieve(n, true);
    for (int i = 0; ++i != n;) {
        a[i] -= a[0];
    }
    for (int p = 2; p < n; ++p) {
        if (sieve[p]) {
            for (int k = (n - 1) / p; k != 0; --k) {
                sieve[k * p] = false;
                a[k * p] -= a[k];
            }
        }
    }

}

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

template <class T>
void multiple_transform(vector<T> &a) {
    int n = a.size();
    vector<bool> sieve(n, true);
    for (int p = 2; p < n; ++p) {
        if (sieve[p]) {
            for (int k = (n - 1) / p; k != 0; --k) {
                sieve[k * p] = false;
                a[k] += a[k * p];
            }
        }
    }
    for (int i = 0; ++i != n;) {
        a[i] += a[0];
    }
}

template <class T>
void inverse_multiple_transform(vector<T> &a) {
    int n = a.size();
    vector<bool> sieve(n, true);
    for (int i = 0; ++i != n;) {
        a[i] -= a[0];
    }
    for (int p = 2; p < n; ++p) {
        if (sieve[p]) {
            for (int k = 1; k * p < n; ++k) {
                sieve[k * p] = false;
                a[k] -= a[k * p];
            }
        }
    }
}


int main() {
  cin >> N;
  A.resize(N);
  G.resize(N);
  rep(i,0,N) cin >> A[i];
  rep(i,0,N - 1) {
    i64 u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  rep(i,0,N) {
    for(i64 d = 1; d * d <= A[i]; d++) {
      if(A[i] % d == 0) {
        dv[d].push_back(i);
        if(A[i] / d != d) {
          dv[A[i] / d].push_back(i);
        }
      }
    }
  }
  vector<fp> ans(dv.size());
  vis.resize(N, 0);
  rep(g,1,dv.size()) {
    for(auto v: dv[g]){
      if(vis[v] == 0) {
        vector<i64> cnt;
        dfs(v, -1, g, cnt);
        rep(i,0,cnt.size() - 1) {
          ans[g] += fp(cnt[i]) * fp(cnt.back() - cnt[i]);
        }
        ans[g] += fp(cnt.back()) * fp(cnt.back() - 1) / fp(2);
      }
    }
    for(auto v: dv[g]) {
      vis[v] = 0;
    }
  }
  inverse_multiple_transform(ans);
  fp res;
  rep(i,1,dv.size()) {
    res += fp(i) * ans[i];
  }
  cout << res << endl;
}

Submission Info

Submission Time
Task G - GCD cost on the tree
User niuez
Language C++ (GCC 9.2.1)
Score 600
Code Size 6081 Byte
Status AC
Exec Time 2476 ms
Memory 74056 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:4:28: warning: unnecessary parentheses in declaration of ‘i’ [-Wparentheses]
    4 | #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
      |                            ^
./Main.cpp:166:3: note: in expansion of macro ‘rep’
  166 |   rep(i,0,N) cin >> A[i];
      |   ^~~
./Main.cpp:4:28: warning: unnecessary parentheses in declaration of ‘i’ [-Wparentheses]
    4 | #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
      |                            ^
./Main.cpp:167:3: note: in expansion of macro ‘rep’
  167 |   rep(i,0,N - 1) {
      |   ^~~
./Main.cpp:4:28: warning: unnecessary parentheses in declaration of ‘i’ [-Wparentheses]
    4 | #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
      |                            ^
./Main.cpp:175:3: note: in expansion of macro ‘rep’
  175 |   rep(i,0,N) {
      |   ^~~
./Main.cpp:4:28: warning: unnecessary parentheses in declaration of ‘g’ [-Wparentheses]
    4 | #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
      |                            ^
./Main.cpp:187:3: note: in expansion of macro ‘rep’
  187 |   rep(g,1,dv.size()) {
      |   ^~~
./Main.cpp:4:42: warning: comparison of integer expressions of different signedness: ‘i64’ {aka ‘long long int’} and ‘std::vector<std::vector<int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
    4 | #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
      |                                      ~~~~^~~~~
./Main.cpp:187:3: note: in expansion of macro ‘rep’
  187 |   rep(g,1,dv.size()) {
      |   ^~~
./Main.cpp:4:28: warning: unnecessary parentheses in declaration of ‘i’ [-Wparentheses]
    4 | #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
      |                            ^
./Main.cpp:192:9: note: in expansion of macro ‘rep’
  192 |         rep(i,0,cnt.size() - 1) {
      |         ^~~
./Main.cpp:4:42: warning: comparison of integer expressions of different signedness: ‘i64’ {aka ...

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 39
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt
Case Name Status Exec Time Memory
example_00.txt AC 10 ms 6280 KiB
example_01.txt AC 10 ms 6356 KiB
hand_00.txt AC 827 ms 73132 KiB
hand_01.txt AC 1676 ms 57992 KiB
hand_02.txt AC 1641 ms 63752 KiB
hand_03.txt AC 600 ms 22384 KiB
hand_04.txt AC 494 ms 21916 KiB
hand_05.txt AC 1383 ms 72856 KiB
hand_06.txt AC 1516 ms 66632 KiB
hand_07.txt AC 2100 ms 66968 KiB
hand_08.txt AC 2093 ms 66588 KiB
hand_09.txt AC 535 ms 28108 KiB
hand_10.txt AC 675 ms 37396 KiB
hand_11.txt AC 1744 ms 65976 KiB
hand_12.txt AC 2088 ms 64408 KiB
hand_13.txt AC 675 ms 38428 KiB
hand_14.txt AC 8 ms 6404 KiB
hand_15.txt AC 8 ms 6232 KiB
random_00.txt AC 2086 ms 68412 KiB
random_01.txt AC 601 ms 28492 KiB
random_02.txt AC 1360 ms 72864 KiB
random_03.txt AC 1620 ms 74056 KiB
random_04.txt AC 1274 ms 69780 KiB
random_05.txt AC 1295 ms 69504 KiB
random_06.txt AC 1343 ms 71552 KiB
random_07.txt AC 1898 ms 61024 KiB
random_08.txt AC 588 ms 22504 KiB
random_09.txt AC 883 ms 67340 KiB
random_10.txt AC 2476 ms 67968 KiB
random_11.txt AC 805 ms 63960 KiB
random_12.txt AC 2385 ms 64532 KiB
random_13.txt AC 867 ms 63908 KiB
random_14.txt AC 2145 ms 60412 KiB
random_15.txt AC 609 ms 22080 KiB
random_16.txt AC 1316 ms 67164 KiB
random_17.txt AC 1182 ms 66532 KiB
random_18.txt AC 1199 ms 63364 KiB
random_19.txt AC 1092 ms 63504 KiB
random_20.txt AC 1264 ms 63980 KiB