CF-60D - Savior(并查集+数论)

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情

D - Savior
Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Misha decided to help Pasha and Akim be friends again. He had a cunning plan — to destroy all the laughy mushrooms. He knows that the laughy mushrooms can easily burst when they laugh. Mushrooms grow on the lawns. There are a[t] mushrooms on the t-th lawn.

Misha knows that the lawns where the mushrooms grow have a unique ability. A lawn (say, i) can transfer laugh to other lawn (say, j) if there exists an integer (say, b) such, that some permutation of numbers a[i], a[j] and b is a beautiful triple (i ≠ j). A beautiful triple is such three pairwise coprime numbers x, y, z, which satisfy the following condition: x2 + y2 = z2.

Misha wants to know on which minimal number of lawns he should laugh for all the laughy mushrooms to burst.

Input

The first line contains one integer n (1 ≤ n ≤ 106) which is the number of lawns. The next line contains n integers ai which are the number of mushrooms on the i-lawn (1 ≤ ai ≤ 107). All the numbers are different.

Output

Print a single number — the minimal number of lawns on which Misha should laugh for all the mushrooms to burst.

Sample Input

Input
1
2
Output
1
Input
2
1 2
Output
2
Input
2
3 5
Output
1

思路:所有勾股数都可以表示为x^2-y^2,,2*x*y,,,x^2+y^2  枚举所有勾股数,然后把能合并的合并,最后查看有几个不同的集合就是答案。


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=1e7+9;
int root[mm],f[mm],n,num;
int look(int x)
{
  if(x^root[x])
    root[x]=look(root[x]);
  return root[x];
}
void uni(int a,int b)
{
  a=look(a);b=look(b);
  if(root[a]!=0&&root[b]!=0&&a!=b)
  {
    root[a]=b;++num;
  }
}
int gcd(int a,int b)
{ int z;
  while(a)
  {
    z=a;a=b%a;b=z;
  }
  return b;
}
int main()
{ long long x,y,z;
  while(scanf("%d",&n)!=EOF)
    { memset(root,0,sizeof(root));
      for(int i=0;i<n;++i)
        {scanf("%d",&f[i]);root[f[i]]=f[i];}
        num=0;
      for(long long i=1;i<mm;i++)
      {
        for(long long j=i+1;2*i*j<mm;j+=2)
        {
          x=j*j-i*i;y=2*i*j;z=i*i+j*j;
          if(x>mm||y>mm)break;
          if(gcd(i,j)!=1)continue;
            uni(x,y);
          if(z<mm)
            uni(x,z);
          if(z<mm)
            uni(y,z);
        }
      }
      printf("%d\n",n-num);
    }
}



研发流程后端开发工具,提示功能开发效率 后端开发工具 立即下载

相关推荐

别再搞错了!ERA5-Land小时数据里的辐射值,原来不是你想的那个‘瞬时值’

本文深入解析ERA5-Land小时数据中辐射值的累积特性,揭示其与瞬时值的本质区别。通过详细的数据处理方法和Python代码示例,指导研究者正确转换累积辐射量为实用瞬时值,避免常见误区,提升气候模型验证的准确性。

weixin_28700411的博客 210

【数论+并查集】Walk on Graph

题目 有一张n个点m条边的无向连通图G,每条边有长度ci,有一个人在上面走 有q组询问,每组询问给出si,ti,ri,表示问你是否存在一条从si出发到ti结束长度为ri%Mod的路径 注意这里的路径长度是∑ci*2^i n,m,q<=50000,Mod<=1000000且Mod为奇数 思路 考虑把这个过程倒过来,这样每走一次就会变成 2x+w2x + w2x+w。 朴素做法是判断到某个点,值为 xxx 是否可行,考虑寻找一些性质来优化这个做法。 不难发现直接做的话是单向边,这样处理起来比较困难。

Brute♂force 370

智能排班考勤系统原型

可实现从业务预测,到工时预估,再到人员分配全流程自动智能排班。可应用于客服行业或工厂的智能排班系统。

gywm15111336483的专栏 995

240. 食物链(并查集+数论)

动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。 A吃B, B吃C,C吃A。 现有N个动物,以1-N编号。 每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是”1 X Y”,表示X和Y是同类。 第二种说法是”2 X Y”,表示X吃Y。 此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。 当一句话满足下列三条之一时,这句话就是假话,否则就是真话。 1)...

marcushbs的博客 557

3GPP Release与UE Category:无线通信标准演进与终端能力分级全解析

在蜂窝移动通信领域,技术标准的演进与终端设备的能力分级是理解整个产业发展的核心框架。其基本原理由国际标准组织3GPP通过周期性发布的技术规范版本(Release)来定义,每个Release都像一份阶段性的技术蓝图,规定了网络与终端应支持的新功能与性能基线。这种标准化机制的技术价值在于确保了全球设备的互联互通与网络的平滑升级,为从4G LTE到5G NR乃至未来的演进提供了清晰路径。在这一框架下,终端能力分级(如LTE时代的UE Category)是将标准转化为具体设备性能的关键环节,它直接定义了终端在射频和

weixin_34098209的博客 331

开放平台 - 互动玩法演进之路

随着直播业务和用户规模日益壮大,如何丰富直播间内容、增强直播间内用户互动效果,提升营收数据变得更加关键。

bilibili_TC的博客 1740

并查集

并查集(Union-find Sets)是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题。一些常见的用途有求连通子图、求最小生成树的 Kruskal 算法和求最近公共祖先(Least Common Ancestors, LCA)等。 使用并查集时,首先会存在一组不相交的动态集合 $S = \left\{ {{S_1},{S_2}, \cdots ,{S_k}} \...

didiao7566的专栏 312

【网络安全的神秘世界】解决dvwa靶场报错:Illegal mix of collations for operation ‘UNION‘

我的 information_schema 使用的是 utf8_general_ci。请根据您的实际情况调整字符集。这通常意味着在执行 UNION 操作时,不同字符集的数据发生了混合,导致冲突。修改字符集设置后,重启 MySQL 服务以应用更改,再次测试一下发现成功。查看 information_schema 的字符集。修改 DVWA 数据库字符集。

菜鸟小羊的博客 2044

pku 1681 Painter's Problem(高斯消元)

Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3462   Accepted: 1710 Description There is a square wall which is made of n*n small square br

nealgavin的专栏 1951

基于YOLOv11与相机标定的齿轮尺寸测量系统

本文详细介绍了一种基于深度学习的目标检测算法YOLOv11与相机标定技术相结合的齿轮尺寸自动测量系统。该系统能够实时检测图像中的齿轮目标,并精确测量其关键尺寸参数,包括齿顶圆直径、齿根圆直径、模数等。我们构建了完整的深度学习解决方案,包含数据采集与标注、模型训练、相机标定、尺寸测量算法以及用户友好的UI界面。本文提供了完整的代码实现,并推荐了相关的参考数据集,为工业视觉检测领域提供了实用的技术方案。关键词:YOLOv11;齿轮检测;尺寸测量;相机标定;深度学习;工业视觉python。

YOLO 411

老 Intel Mac mini 装 OpenClaw:低成本常年在线方案

老款Intel Mac mini部署OpenClaw方案摘要 本文详细介绍了如何利用老款Intel Mac mini低成本部署OpenClaw系统。方案推荐2011-2018款Mac mini,建议升级至16GB内存和SSD存储以提升性能。部署步骤包括:1)安装Ubuntu系统;2)配置Docker环境;3)通过docker-compose部署OpenClaw及其依赖组件(PostgreSQL和Redis);4)进行系统级和Docker层面的性能优化;5)设置开机自启动服务。该方案充分利用老款设备剩余价值,

一键难忘的博客 2477

pku 1753 Flip Game(高斯消元+枚举)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20199   Accepted: 8752 Description Flip game is played on a rectangular 4x4 field with two-sided piec

nealgavin的专栏 1660

OpenCASCADE:Direct3D CSharp 示例

OpenCASCADE:Direct3D CSharp 示例OpenCASCADE:Direct3D CSharp 示例 OpenCASCADE:Direct3D CSharp 示例 此示例演示了如何在使用CSharp和Windows Presentation Foundation (WPF)编写的.Net应用程序中使用 OCCT 和 DirectX 库。 .Net、OCCT (C++) 和 DirectX 级别之间的连接由用 C++/CLI 编写的代理库OCCProxy和D3DProxy提供。代理库OCC

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1827

hdu 1250 Hat's Fibonacci

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3726    Accepted Submission(s): 1252 Problem Description A Fibonacci se

nealgavin的专栏 1251

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

nefu702 The minimum square sum(数论)

The minimum square sum Time Limit 1000ms Memory Limit 65536K description   Given a prime p (p8),you are to find min{x2+y2},where x and y belongs t

nealgavin的专栏 1244

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

CF-50E-Square Equation Roots(数论)

E - Square Equation Roots Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 50E Description A schoolboy Petya studies sq

nealgavin的专栏 1107

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

hdu 4357 String change(推理题,4级)

String change Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 802    Accepted Submission(s): 370 Problem Description In this proble

nealgavin的专栏 1010

USACO section 4.1 Beef McNuggets(数论+背包)

Beef McNuggets Hubert Chen Farmer Brown's cows are up in arms, having heard that McDonalds is considering the introduction of a new product: Beef McNuggets. The cows are trying to find any possibl

nealgavin的专栏 988

LS 38 Geometric sum(数论+二分快速幂)

Geometric sum Compute (a+a2+…an)modm. Input Three integers a,n,m. (1≤a,n,m≤1018) Output The only integer denotes the result. Sample input 2 2 1000000000 Sample output 6

nealgavin的专栏 979

hdu 4465 Candy(概率公式+极大乘极小,4级)

Candy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1016    Accepted Submission(s): 449 Special Judge Problem Description LazyChil

nealgavin的专栏 954

hdu 1042 N!

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 33314    Accepted Submission(s): 9234 Problem Description                Given an in

nealgavin的专栏 926

CF 17D - Notepad(数论取模+欧拉函数)

D - Notepad Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 17D Description Nick is attracted by everything unconventio

nealgavin的专栏 900

素数判断(数论)

素数判定 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 8   Accepted Submission(s) : 2 Font: Times New Roman | Verdana | Georgia Font Size: ← →

nealgavin的专栏 895

hdu 3117 Fibonacci Numbers

Fibonacci Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 993    Accepted Submission(s): 424 Problem Description The Fibonacci

nealgavin的专栏 845
上一篇: hdu 4502 吉哥系列故事——临时工计划(DP)
下一篇: hdu 4512 吉哥系列故事——完美队形I(最长公共上升序列)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值