2021牛客暑期多校训练营3 J. Counting Triangles(容斥原理)
链接:https://ac.nowcoder.com/acm/contest/11254/J
来源:牛客网
题目描述
Goodeat finds an undirected complete graph with n vertices. Each edge of the graph is painted black or white. He wants you to help him find the number of triangles (a, b, c) (a < b < c), such that the edges between (a, b), (b, c), (c, a) have the same color. To avoid the input scale being too large, we use the following code to generate edges in the graph. namespace GenHelper { unsigned z1,z2,z3,z4,b,u; unsigned get() { b=((z1<<6)^z1)>>13; z1=((z1&4294967294U)<<18)^b; b=((z2<<2)^z2)>>27; z2=((z2&4294967288U)<<2)^b; b=((z3<<13)^z3)>>21; z3=((z3&4294967280U)<<7)^b; b=((z4<<3)^z4)>>12; z4=((z4&4294967168U)<<13)^b; return (z1^z2^z3^z4); } bool read() { while (!u) u = get(); bool res = u & 1; u >>= 1; return res; } void srand(int x) { z1=x; z2=(~x)^0x233333333U; z3=x^0x1234598766U; z4=(~x)+51; u = 0; } } using namespace GenHelper; bool edge[8005][8005]; int main() { int n, seed; cin >> n >> seed; srand(seed); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) edge[j][i] = edge[i][j] = read(); return 0; }
The edge array in the above code stores the color of the edges in the graph. edge[i][j]=1 means that the edge from i to j is black, otherwise it is white (∀0≤i≠j≤n−1∀0≤i\=j≤n−1).
Ensure that there is an approach that does not depend on the way the data is generated.
输入描述:
The first line contains two integers n(n≤8000),seed(seed≤109)n(n≤8000),seed(seed≤109), denote the number of vertices and the seed of random generator.
输出描述:
Output a line denoting the answer.
示例1
输入
复制
10 114514
输出
复制
35
说明
There're 35 triangles that all three edges have the same color.
考虑容斥,直接用总数减不满足条件的三角形的个数。总数就是,且可以注意到不满足条件的三角形一定是两个边为0一个边为1或者两个边为1一个边为0,即一个三角形中有两个所有关联边异色的顶点和一个所有关联边同色的顶点。然后可以枚举顶点统计关联边异色的顶点个数,除以2就是不满足条件的三角形个数。
#include <bits/stdc++.h>
namespace GenHelper
{
unsigned z1,z2,z3,z4,b,u;
unsigned get()
{
b=((z1<<6)^z1)>>13;
z1=((z1&4294967294U)<<18)^b;
b=((z2<<2)^z2)>>27;
z2=((z2&4294967288U)<<2)^b;
b=((z3<<13)^z3)>>21;
z3=((z3&4294967280U)<<7)^b;
b=((z4<<3)^z4)>>12;
z4=((z4&4294967168U)<<13)^b;
return (z1^z2^z3^z4);
}
bool read() {
while (!u) u = get();
bool res = u & 1;
u >>= 1; return res;
}
void srand(int x)
{
z1=x;
z2=(~x)^0x233333333U;
z3=x^0x1234598766U;
z4=(~x)+51;
u = 0;
}
}
using namespace GenHelper;
using namespace std;
bool edge[8005][8005];
int main() {
int n, seed;
cin >> n >> seed;
srand(seed);
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
edge[j][i] = edge[i][j] = read();
}
}
long long cnt = 0, all = 0;
for(int i = 1; i <= n; i++) {
long long tmp = 0;
long long x = 0, y = 0;
for(int j = 1; j <= n; j++) {
if(i == j) continue;
if(edge[i][j]) x++;
else y++;
}
cnt += x * y;
}
cout << 1ll * n * (n - 1) * (n - 2) / 6 - cnt / 2;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!