Tenka1 Programmer Contest 2019 E - Polynomial Divisors
关于check()函数,相隔p-1取出来,提出一个公因数后,剩下的为\(x^{p-1},x^{2*(p-1)}…x^{n*(p-1)}\),x与p互质,各项的幂取余p的欧拉函数为0。
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 100;
const int inf = 0x3f3f3f3f;
vector<pair<int, int> > e[maxn];
int n, k;
int vis[maxn];
int xorr[maxn];
void read(int &x) {
x = 0;
char ch, c = getchar();
while (c < '0' || c > '9') ch = c, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
if (ch == '-') x = -x;
}
void dfs(int now) {
if (vis[now]) return;
vis[now] = 1;
for (auto x:e[now]) {
if (!vis[x.first]) {
xorr[x.first] = xorr[now] ^ x.second;
dfs(x.first);
}
}
}
unordered_map<int, int> mp;
int main() {
// freopen("in.txt", "r", stdin);
read(n);
read(k);
int x, y, z;
for (int i = 1; i < n; i++) {
read(x);
read(y);
read(z);
e[x].push_back({y, z});
e[y].push_back({x, z});
}
dfs(1);
for (int i = 1; i <= n; i++) {
mp[xorr[i]]++;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
mp[xorr[i]]--;
x = xorr[i] ^ k;
ans += mp[x];
mp[xorr[i]]++;
}
cout << ans / 2 << endl;
return 0;
}