#include <bits/stdc++.h> typedef long long ll; const int maxx = 100010; const int inf = 0x3f3f3f3f; const int mod = 1000000009; using namespace std; map<ll,ll>mapp; ll a[maxx]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n, p; cin >> t; while (t--) { cin >> n >> p; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] %= p; } int ans = 0, sum = 0; mapp[0] = 1; for (int i = 1; i <= n; i++) { sum += a[i]; sum %= p; if (mapp[sum] != 0) { ans++; mapp.clear(); mapp[0] = 1; sum = 0; } else mapp[sum] = 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> typedef long long ll; const int maxx = 100010; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; using namespace std; int a[maxx]; int pre[maxx]; int f[maxx][2]; int num[2]; int getf(int a) { if (a == pre[a]) return a; return pre[a] = getf(pre[a]); } int mer(int a, int b) { int fa = getf(a); int fb = getf(b); if (fa == fb) return fa; pre[fb] = fa; f[fa][0] += f[fb][0]; f[fa][1] += f[fb][1]; return fa; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { memset(f, 0, sizeof(f)); num[0] = num[1] = 0; int n; cin >> n; for (int i = 1; i <= n; i++) { pre[i] = i; cin >> a[i]; if (a[i] == 1) { num[0]++; f[i][0] = 1; } if (a[i] == 2) { num[1]++; f[i][1] = 1; } } ll ans = 1ll * num[1] * (num[1] - 1) / 2 % mod * num[0] % mod + 1ll * num[1] * (num[1] - 1) * (num[1] - 2) / 6 % mod; cout << ans % mod << endl; for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; if (i >= n - 2) { cout << "0" << endl; continue; } int fa = getf(u); int fb = getf(v); int u1 = f[fa][0], u2 = f[fa][1]; int v1 = f[fb][0], v2 = f[fb][1]; int cnt = mer(u, v); int x = f[cnt][0]; int y = f[cnt][1]; ans -= 1ll * u1 * v2 % mod * (num[1] - y) % mod; if (ans < 0) ans += mod; ans -= 1ll * u2 * v1 % mod * (num[1] - y) % mod; if (ans < 0) ans += mod; ans -= 1ll * u2 * v2 % mod * (n - x - y) % mod; if (ans < 0) ans += mod; cout << ans % mod << endl; } } return 0; }
#include <bits/stdc++.h> typedef long long ll; const int maxx = 110; const int inf = 0x3f3f3f3f; const int mod = 1000000009; using namespace std; int g[maxx][maxx], pre[6][maxx], dis[maxx]; bool vis[maxx]; int n, ans; void Dijkstra(int m) { memset(dis, inf, sizeof(int) * (n + 5)); memset(vis, false, n + 5); dis[1] = 0; for (int i = 1; i < n; i++) { int minn = inf; int u = -1; for (int j = 1; j <= n; j++) { if (!vis[j] && dis[j] < minn) { minn = dis[j]; u = j; } } vis[u] = true; for (int v = 1; v <= n; v++) { if (dis[v] > dis[u] + g[u][v]) { dis[v] = dis[u] + g[u][v]; pre[m][v] = u; } } } } void dfs(int m) { Dijkstra(m); if (!m) { ans = max(ans, dis[n]); return; } int pos = n; while (pos != 1) { int u = pos, v = pre[m][pos]; int temp = g[u][v]; g[u][v] = g[v][u] = inf; dfs(m - 1); g[u][v] = g[v][u] = temp; pos = pre[m][pos]; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int m; cin >> n >> m; for (int i = 1; i <= n * (n - 1) / 2; i++) { int u, v, w; cin >> u >> v >> w; g[u][v] = g[v][u] = w; } ans = 0; dfs(m); cout << ans << endl; } return 0; }