Counting Triangles(逆向思维)2021牛客暑期多校训练营3
作者:互联网
J. Counting Triangles
题意:
(a, b, c) (a < b < c),(a, b), (b, c), (c, a) 三边同时为true or false 计数++
复盘:
a -> b -> c -> a , 形成了一个三角形,首先肯定能想到 n 三次暴力枚举,必然超时,想半天也没什么思路,经队友提醒,首先是无向图(一直拿有向图去写),然后三角形的三边情况就四种,全1,全0,两1,两0。三角形总个数为Cn取3,去算不计数的个数,不计数的个数为:对于每一个点,分别计数其到剩下的点,边权为0和1的个数,两者相乘即为不计数个数,遍历一遍求sum再/2(i到j,j到i 重复了)
#include <bits/stdc++.h>
#define int long long
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];
signed 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();
int sum=0;
for(int i=0;i<n;i++)
{
int se=0, sl=0;
for(int j=0;j<n;j++)
{
if(i == j) continue;
if(edge[i][j]) se++;
else sl++;
}
sum += se*sl;
}
cout<<n*(n-1)*(n-2)/6-sum/2<<endl;
return 0;
}
标签:int,++,多校,牛客,2021,z4,z1,z2,z3 来源: https://blog.csdn.net/qq_51354600/article/details/119064140