传送门 J-Counting Triangles_2021牛客暑期多校训练营3 (nowcoder.com)

题目

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;
}

  

题意

给出两个数, n为几个顶点, 运行题目代码后, edge数组 edge[i][j]==1的道路为黑色 ==0的道路为白, 只有同色的才能相连构成三角形, 求构成三角形的个数

题解

一. n*(n-1)*(n-2)/6是C3n ,即n个数可以组成的全部三角形数目(无颜色差别, 1为黑 0为白)

二. 每个三角形三条边的颜色可以为 111    000   011   001 其中前二者为所求, 后二者为所弃。 若要直接求, 比较复杂,

但后二者有简单规律:必有两个顶点使得 它的两个邻边为0和1

方法:枚举1~n的点x, (连接x为1的和连接x为0)连成三角形就是后二者的情况

最后结果即为n个数可以组成的全部三角形数目 - 无法构成的三角形

代码

#include <iostream>

using namespace std;

typedef long long LL;
const int N = 2e5+10;
LL st[N];
LL res = 0; //此为题中所给代码
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(); //从此开始为手写代码
LL ans = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++)
if(edge[i][j])
st[i] ++;
for(int i = 0; i < n; i ++)
ans += st[i] * (n-1-st[i]);
cout << (LL)n*(LL)(n-1)*(n-2)/6 - ans/2;
return 0;
}

2021牛客暑期多校训练营3 J 思维的更多相关文章

  1. 2021牛客暑期多校训练营9C-Cells【LGV引理,范德蒙德行列式】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/11260/C 题目大意 一个平面上,\(n\)个起点\((0,a_i)\)分别对应终点\((i,0)\),每次 ...

  2. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  3. B-xor_2019牛客暑期多校训练营(第四场)

    题意 给出n个数组(每组数个数不定),m个询问 l, r, x 序号在区间\([l,r]\)的每个数组是否都可以取出任意个数异或出x 题解 判断一个数组能否异或出x,是简单的线性基问题 判断多个线性基 ...

  4. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  5. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  6. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  7. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  8. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  9. 2019牛客暑期多校训练营(第八场)E.Explorer

    链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...

随机推荐

  1. SpringBoot中常用的45个注解

    1.SpringBoot/spring @SpringBootApplication: 包含@Configuration.@EnableAutoConfiguration.@ComponentScan ...

  2. Anaconda Navigator卡logo打不开闪退问题处理方案-更换阿里云镜像源

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 一.打开软件卡logo,点击图标后闪退 最近有同事使用anaconda时出现了卡logo,显示loading applications,点击图标时发 ...

  3. ansible 五 playbooks剧本使用

    一.Playbook 简介 Playbooks与Ad-Hoc相比,是一种完全不同的运用Ansible的方式,而且是非常之强大的:也是系统ansible命令的集合,其利用yaml语言编写,运行过程,an ...

  4. Redis+Caffeine两级缓存,让访问速度纵享丝滑

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 在高性能的服务架构设计中,缓存是一个不可或缺的环节.在实际的项目中,我们通常会将一些热点数据存储到Redis或MemCache这类缓存中间件中, ...

  5. CyclicBarrier和CountDownLatch区别

    这两天写多线程时,用到了CyclicBarrier,下意识的认为CyclicBarrier和CountDownLatch作用很像,就翻阅资料查了一下,说一下他们的区别吧 CyclicBarrier和C ...

  6. HashMap:为什么容量总是为2的n次幂

    HashMap:为什么容量总是为2的n次幂1).HashMap是根据key的hash值决定key放到哪个桶中,通过tab[i = (n - 1) & hash]公式计算得出 这里的n是Hash ...

  7. vue解除双向绑定?

    let obj = JSON.parse(JSON.stringify(this.temp1));

  8. Spring Boot 2.X 有什么新特性?与 1.X 有什么区别?

    配置变更JDK 版本升级第三方类库升级响应式 Spring 编程支持HTTP/2 支持配置属性绑定更多改进与加强-

  9. Shiro Session放到Redis中常遇到的问题

    Shiro会话管理:https://shiro.apache.org/session-management.html#SessionManagement-CustomSessionIDs Redis主 ...

  10. 面试问题之计算机网络:HTTP和HTTPS的区别

    https://blog.csdn.net/qq_38289815/article/details/80969419