博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF-60D - Savior(并查集+数论)
阅读量:4928 次
发布时间:2019-06-11

本文共 1998 字,大约阅读时间需要 6 分钟。

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
12
Output
1
Input
21 2
Output
2
Input
23 5
Output
1

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

#include
#include
#include
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
mm||y>mm)break; if(gcd(i,j)!=1)continue; uni(x,y); if(z

转载于:https://www.cnblogs.com/nealgavin/archive/2013/03/22/3205958.html

你可能感兴趣的文章
Vue与React的异同
查看>>
360:跳高游戏
查看>>
CSS3 Background-size
查看>>
Python Ethical Hacking - MAC Address & How to Change(3)
查看>>
生成验证码
查看>>
深入理解计算机系统 第2章 信息的表示和处理
查看>>
JS中数据结构之链表
查看>>
Tomcat 配置文件 (server.xml)详解--转载
查看>>
Golang理解-字符串拼接的几种方式
查看>>
隐式激活Activity
查看>>
DAG
查看>>
使用MVVM框架时,如何处理在页面动态渲染完之后需要发生的事件呢?
查看>>
StringBuilder与StringBuffer的区别
查看>>
String、StringBuffer、StringBuilder
查看>>
(转)导出EXCEL时科学计数法问题
查看>>
SVN客户端与服务器端搭建
查看>>
知识点整理一
查看>>
判断浏览器
查看>>
python并发编程之进程池,线程池concurrent.futures
查看>>
rdd的元素打印
查看>>