52642023-04-24 17:42:46ZsofiaKeresztelyTestvérvárosokcpp14Accepted 100/100703ms358388 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define se second
#define fi first
vector<vector<pii> > g;
vector<vector<int> > ls;
int n, k;

int dfs(int p, int v){
    int r = 0;
    ls[v][0] = 1;
    for (pii x : g[v]){
        if (x.fi == p) continue;
        r += dfs(v, x.fi);
        for (int i=0; i<k; i++){
            ls[v][(i+x.se)%k] += ls[x.fi][i];
        }
    }
    r *= 2;
    for (pii x : g[v]){
        if (x.fi == p) continue;
        for (int i=0; i<k; i++){
            r += (ls[v][i] - ls[x.fi][(i-x.se+500*k)%k]) * ls[x.fi][(501*k-i-x.se)%k];
        }
    }
    r += ls[v][0] - 1;
    return r/2;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> k;
    g.resize(n+1);
    ls.assign(n+1, vector<int> (k, 0));
    for (int i=0; i<n-1; i++){
        int a, b, w;
        cin >> a >> b >> w;
        g[a].push_back({b, w});
        g[b].push_back({a, w});
    }
    cout << dfs(0, 1);
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted3ms2236 KiB
subtask215/15
3Accepted3ms2812 KiB
4Accepted3ms2556 KiB
5Accepted4ms3448 KiB
6Accepted13ms7908 KiB
7Accepted3ms3060 KiB
8Accepted8ms6228 KiB
9Accepted4ms4040 KiB
subtask315/15
10Accepted10ms6132 KiB
11Accepted4ms4372 KiB
12Accepted37ms15168 KiB
13Accepted32ms15172 KiB
14Accepted37ms17404 KiB
subtask420/20
15Accepted4ms7440 KiB
16Accepted17ms14148 KiB
17Accepted48ms30300 KiB
18Accepted172ms98008 KiB
19Accepted131ms73204 KiB
20Accepted108ms65676 KiB
21Accepted28ms25632 KiB
22Accepted689ms358388 KiB
subtask550/50
23Accepted537ms266564 KiB
24Accepted312ms160544 KiB
25Accepted317ms161560 KiB
26Accepted126ms72532 KiB
27Accepted203ms113304 KiB
28Accepted185ms97500 KiB
29Accepted703ms355580 KiB
30Accepted34ms24444 KiB
31Accepted588ms314312 KiB