Submission #558844

# Submission time Handle Problem Language Result Execution time Memory
558844 2022-05-08T17:10:43 Z RaresFelix Star Trek (CEOI20_startrek) C++17
100 / 100
87 ms 18684 KB
#include <bits/stdc++.h>
#define MN 100071
#define MOD 1000000007

using namespace std;
using ll = long long;

ll n, d;
vector<int> L[MN];

ll logp(ll a, ll e) {
    return !e ? 1 : 
        (e & 1) ? a * logp(a * a % MOD, e >> 1) % MOD :
                    logp(a * a % MOD, e >> 1);
}


ll alpha, beta_c, lambda0, state0;
namespace ARB {
    ll NrFiiL[MN], NrC[2][MN];
    ll StateRad[MN], C[MN];
    void dbgTree() {
        for(int i = 1; i <= n; ++i) printf("Nod %d - %d %d %d - %d %d\n", i, NrFiiL[i], NrC[0][i], NrC[1][i], StateRad[i], C[i]);
        printf("--------\n");
    }
    inline bool stare(int u) {
        return !!NrFiiL[u];
    }
    inline ll fnrc(int u) {
        if(NrFiiL[u] > 1) return 0;
        if(NrFiiL[u] == 1) return NrC[0][u];
        return NrC[1][u] + 1;
    }
    void dfs_init(int u, int p) {
        for(auto it : L[u])
            if(it != p) {
                dfs_init(it, u);
                NrFiiL[u] += !stare(it);
            }
        for(auto it : L[u])
            if(it != p) {
                NrC[stare(it)][u] += fnrc(it);
            }
    }
    void move_root(int u, int v) {
        NrFiiL[u] -= !stare(v);
        NrC[stare(v)][u] -= fnrc(v);
        NrC[stare(u)][v] += fnrc(u);
        NrFiiL[v] += !stare(u);
    }
    void dfs_ch_root(int u, int p) {
        //radacina este in u
        C[u] = fnrc(u);
        StateRad[u] = stare(u);
        for(auto it : L[u]) if(it != p) {
            move_root(u, it);
            dfs_ch_root(it, u);
            move_root(it, u);
        }
    }

    void init() {
        dfs_init(1, 0);
        dfs_ch_root(1, 0);
        beta_c = 0;
        for(int i = 1; i <= n; ++i) {
            alpha += (StateRad[i] ? 1 : -1) * C[i];
            alpha = (alpha % MOD + MOD) % MOD;
            beta_c += !StateRad[i];
            lambda0 += !StateRad[i];
        }
        beta_c = beta_c * logp(n, 2 * d);
    }
}

pair<ll, ll> conv(ll a, ll b, ll nr) {
    if(!nr) return {1, 0};
    auto [a1, b1] = conv(a, b, nr >> 1);
    ll a2 = 1ll * a1 * a1 % MOD, b2 = b1 * (a1 + 1) % MOD; 
    if(nr & 1) {
        a1 = a2, b1 = b2;
        a2 = a1 * a, b2 = (b1 * a + b) % MOD;
    }
    return {a2, b2};
}

ll inv(ll a) {
    return logp(a, MOD - 2);
}


int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> d;
    for(int i = 1; i < n; ++i) {
        ll u, v;
        cin >> u >> v;
        L[u].push_back(v);
        L[v].push_back(u);
    }
    ARB::init();
    ll lambda_d1 = logp(n, 2 * d) - logp(alpha, d);
    lambda_d1 = (lambda_d1 % MOD + MOD) % MOD;
    ll div = n * n % MOD - alpha;
    div = (div % MOD + MOD) % MOD;
    div = inv(div);
    lambda_d1 = lambda_d1 * div % MOD * lambda0 % MOD;
    ll rez = 0;
    if(ARB::StateRad[1]) rez = ((logp(n, 2 * d) - lambda_d1 * ARB::C[1] % MOD ) % MOD + MOD ) % MOD;
    else rez = lambda_d1 * ARB::C[1] % MOD;
    cout << rez << "\n";
    return 0;
}

Compilation message

startrek.cpp: In function 'void ARB::dbgTree()':
startrek.cpp:23:55: warning: format '%d' expects argument of type 'int', but argument 3 has type 'll' {aka 'long long int'} [-Wformat=]
   23 |         for(int i = 1; i <= n; ++i) printf("Nod %d - %d %d %d - %d %d\n", i, NrFiiL[i], NrC[0][i], NrC[1][i], StateRad[i], C[i]);
      |                                                      ~^                      ~~~~~~~~~
      |                                                       |                              |
      |                                                       int                            ll {aka long long int}
      |                                                      %lld
startrek.cpp:23:58: warning: format '%d' expects argument of type 'int', but argument 4 has type 'll' {aka 'long long int'} [-Wformat=]
   23 |         for(int i = 1; i <= n; ++i) printf("Nod %d - %d %d %d - %d %d\n", i, NrFiiL[i], NrC[0][i], NrC[1][i], StateRad[i], C[i]);
      |                                                         ~^                              ~~~~~~~~~
      |                                                          |                                      |
      |                                                          int                                    ll {aka long long int}
      |                                                         %lld
startrek.cpp:23:61: warning: format '%d' expects argument of type 'int', but argument 5 has type 'll' {aka 'long long int'} [-Wformat=]
   23 |         for(int i = 1; i <= n; ++i) printf("Nod %d - %d %d %d - %d %d\n", i, NrFiiL[i], NrC[0][i], NrC[1][i], StateRad[i], C[i]);
      |                                                            ~^                                      ~~~~~~~~~
      |                                                             |                                              |
      |                                                             int                                            ll {aka long long int}
      |                                                            %lld
startrek.cpp:23:66: warning: format '%d' expects argument of type 'int', but argument 6 has type 'll' {aka 'long long int'} [-Wformat=]
   23 |         for(int i = 1; i <= n; ++i) printf("Nod %d - %d %d %d - %d %d\n", i, NrFiiL[i], NrC[0][i], NrC[1][i], StateRad[i], C[i]);
      |                                                                 ~^                                            ~~~~~~~~~~~
      |                                                                  |                                                      |
      |                                                                  int                                                    ll {aka long long int}
      |                                                                 %lld
startrek.cpp:23:69: warning: format '%d' expects argument of type 'int', but argument 7 has type 'll' {aka 'long long int'} [-Wformat=]
   23 |         for(int i = 1; i <= n; ++i) printf("Nod %d - %d %d %d - %d %d\n", i, NrFiiL[i], NrC[0][i], NrC[1][i], StateRad[i], C[i]);
      |                                                                    ~^                                                      ~~~~
      |                                                                     |                                                         |
      |                                                                     int                                                       ll {aka long long int}
      |                                                                    %lld
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2680 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 3 ms 2808 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 3 ms 2808 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 87 ms 14348 KB Output is correct
13 Correct 71 ms 18592 KB Output is correct
14 Correct 64 ms 10800 KB Output is correct
15 Correct 69 ms 10880 KB Output is correct
16 Correct 59 ms 10900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 3 ms 2808 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2680 KB Output is correct
17 Correct 2 ms 2676 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Correct 2 ms 2772 KB Output is correct
23 Correct 2 ms 2772 KB Output is correct
24 Correct 2 ms 2772 KB Output is correct
25 Correct 2 ms 2772 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2692 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2676 KB Output is correct
7 Correct 3 ms 2808 KB Output is correct
8 Correct 2 ms 2772 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 87 ms 14348 KB Output is correct
13 Correct 71 ms 18592 KB Output is correct
14 Correct 64 ms 10800 KB Output is correct
15 Correct 69 ms 10880 KB Output is correct
16 Correct 59 ms 10900 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2684 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2680 KB Output is correct
22 Correct 2 ms 2676 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2680 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 2 ms 2772 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 ms 2692 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 69 ms 14460 KB Output is correct
37 Correct 71 ms 18672 KB Output is correct
38 Correct 57 ms 10776 KB Output is correct
39 Correct 68 ms 10916 KB Output is correct
40 Correct 58 ms 10828 KB Output is correct
41 Correct 70 ms 16572 KB Output is correct
42 Correct 60 ms 17300 KB Output is correct
43 Correct 41 ms 9888 KB Output is correct
44 Correct 80 ms 10796 KB Output is correct
45 Correct 71 ms 10888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2676 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2680 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2676 KB Output is correct
14 Correct 3 ms 2808 KB Output is correct
15 Correct 2 ms 2772 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 87 ms 14348 KB Output is correct
20 Correct 71 ms 18592 KB Output is correct
21 Correct 64 ms 10800 KB Output is correct
22 Correct 69 ms 10880 KB Output is correct
23 Correct 59 ms 10900 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2684 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2680 KB Output is correct
29 Correct 2 ms 2676 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2680 KB Output is correct
33 Correct 2 ms 2772 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 2 ms 2772 KB Output is correct
38 Correct 2 ms 2772 KB Output is correct
39 Correct 2 ms 2772 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2692 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 69 ms 14460 KB Output is correct
44 Correct 71 ms 18672 KB Output is correct
45 Correct 57 ms 10776 KB Output is correct
46 Correct 68 ms 10916 KB Output is correct
47 Correct 58 ms 10828 KB Output is correct
48 Correct 70 ms 16572 KB Output is correct
49 Correct 60 ms 17300 KB Output is correct
50 Correct 41 ms 9888 KB Output is correct
51 Correct 80 ms 10796 KB Output is correct
52 Correct 71 ms 10888 KB Output is correct
53 Correct 75 ms 18684 KB Output is correct
54 Correct 76 ms 17036 KB Output is correct
55 Correct 45 ms 9152 KB Output is correct
56 Correct 65 ms 14372 KB Output is correct
57 Correct 62 ms 11084 KB Output is correct
58 Correct 55 ms 10824 KB Output is correct
59 Correct 73 ms 10828 KB Output is correct
60 Correct 60 ms 10888 KB Output is correct