1161 - Extreme GCD
Time Limit: 1 second(s) | Memory Limit: 32 MB |
All of you know that GCD means the greatest common divisor. So, you must have thought that this problem requires finding some sort of GCD. Don't worry, you are absolutely right!
Given N positive integers, not necessarily distinct, how many ways you can take 4 integers from the N numbers such that their GCD is 1.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with an integer N (4 ≤ N ≤ 10000). The next line contains N integers separated by spaces. The integers will be positive and not greater than 10000.
Output
For each case, print the case number and the number of ways you can take the integers as mentioned above.
Sample Input |
Output for Sample Input |
3 4 2 4 6 1 5 1 2 4 6 8 10 12 46 100 131 5 6 7 8 9 10 |
Case 1: 1 Case 2: 4 Case 3: 195 |
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 bool prime[10005]= {0};
12 int ans[10005];
13 int aa[10005];
14 int bt[10005];
15 int cc[10005]= {0};
16 bool dd[10005]= {0};
17 queue<int>que;
18 int main(void)
19 {
20 int i,j,k;
21 for(i=2; i<200; i++)
22 {
23 for(j=i; i*j<=10000; j++)
24 {
25 prime[i*j]=true;
26 }
27 }
28 int cnt=0;
29 for(i=2; i<=10000; i++)
30 {
31 if(!prime[i])
32 {
33 ans[cnt++]=i;
34 }
35 }int d;
36 cin>>d;int s;
37 for(s=1;s<=d;s++)
38 { cin>>k;
39 memset(bt,0,sizeof(bt));
40 for(i=0; i<k; i++)
41 {
42 scanf("%d",&aa[i]);
43 }
44 for(i=0; i<k; i++)
45 {
46 int nn=aa[i];
47 int t=0;
48 int flag=0;
49 while(nn>1)
50 {
51 if(flag==0&&nn%ans[t]==0)
52 {
53 flag=1;
54 que.push(ans[t]);
55 nn/=ans[t];
56 }
57 else if(nn%ans[t]==0)
58 {
59 nn/=ans[t];
60 flag=1;
61 }
62 else
63 {
64 flag=0;
65 t++;
66 }
67 }
68 if(nn>1)
69 {
70 que.push(nn);
71 }
72 int xx=0;
73 while(!que.empty())
74 {
75 cc[xx++]=que.front();
76 que.pop();
77 }
78 int x;
79 int y;
80 for(x=1; x<=(1<<xx)-1; x++)
81 {
82 int ak=1;
83 int vv=0;
84 for(j=0; j<xx; j++)
85 {
86 if(x&(1<<j))
87 {
88 vv++;
89 ak*=cc[j];
90 }
91 }
92 bt[ak]+=1;
93 if(vv%2)
94 dd[ak]=true;
95 }
96 }
97 LL sum=0;
98 LL sum1=0;
99 for(i=2; i<=10000; i++)
100 {
101 if(bt[i]>=4)
102 {
103 LL nn=(LL)bt[i]*(LL)(bt[i]-1)*(LL)(bt[i]-2)*(LL)(bt[i]-3)/24;
104 if(dd[i])
105 sum+=nn;
106 else sum-=nn;
107 }
108 }
109 sum1=(LL)k*(LL)(k-1)*(LL)(k-2)*(LL)(k-3)/24;
110 sum1-=sum;printf("Case %d: ",s);
111 printf("%lld\n",sum1);
112 }
113 return 0;
114 }
1161 - Extreme GCD的更多相关文章
- LightOJ 1161 - Extreme GCD 容斥
题意:给你n个数[4,10000],问在其中任意选四个其GCD值为1的情况有几种. 思路:GCD为1的情况很简单 即各个数没有相同的质因数,所以求所有出现过的质因数次数再容斥一下-- 很可惜是错的,因 ...
- spoj 3871. GCD Extreme 欧拉+积性函数
3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. Th ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)
[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- GCD - Extreme (II) for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } 推导分析+欧拉函数
/** 题目:GCD - Extreme (II) 链接:https://vjudge.net/contest/154246#problem/O 题意: for(i=1;i<N;i++) for ...
- USACO GCD Extreme(II)
题目大意:求gcd(1,2)+gcd(1,3)+gcd(2,3)+...+gcd(n-1,n) ---------------------------------------------------- ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
随机推荐
- 痞子衡嵌入式:利用GPIO模块来测量i.MXRT1xxx的系统中断延迟时间
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1xxx的系统中断延迟时间. 在 <Cortex-M系统中断延迟及其测量方法简介> 一文里,痞子衡介绍了 Cor ...
- jmeter非GUI(cmd命令行)模式的压测和输出测试报告
1.非GUI模式的压测,和GUI有啥不同? 2.非GUI模式怎么搞? 大家打开jmeter的时候,都会看到这个界面: 注意看这句话: Don't use GUI mode for load testi ...
- Netty | 第1章 Java NIO 网络编程《Netty In Action》
目录 前言 1. Java 网络编程 1.1 Javs NIO 基本介绍 1.2 缓冲区 Buffer 1.2 通道 Channel 1.3 选择器 Selector 1.4 NIO 非阻塞网络编程原 ...
- 为构建大型复杂系统而生的微服务框架 Erda Infra
作者|宋瑞国(尘醉) 来源|尔达 Erda 公众号 导读:Erda Infra 微服务框架是从 Erda 项目演进而来,并且完全开源.Erda 基于 Erda Infra 框架完成了大型复杂项目的 ...
- C/C++ Qt 数据库与SqlTableModel组件应用
SqlTableModel 组件可以将数据库中的特定字段动态显示在TableView表格组件中,通常设置QSqlTableModel类的变量作为数据模型后就可以显示数据表内容,界面组件中则通过QDat ...
- Linux下删除的文件如何恢复
Linux下删除的文件如何恢复 参考自: [1]linux下误操作删除文件如何恢复 [2]Linux实现删除撤回的方法 以/home/test.txt为例 1.df -T 文件夹 找到当前文件所在磁盘 ...
- Linux启动初始化配置文件
Linux启动初始化配置文件(1)/etc/profile 登录时,会执行. 全局(公有)配置,不管是哪个用户,登录时都会读取该文件. (2)/ect/bashrc Ubuntu没有此文件,与之对应的 ...
- 集合类——Map集合、Properties属性文件操作
1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...
- Java 设计模式--策略模式,枚举+工厂方法实现
如果项目中的一个页面跳转功能存在10个以上的if else判断,想要做一下整改 一.什么是策略模式 策略模式是对算法的包装,是把使用算法的责任和算法本身分割开来,委派给不同的对象管理,最终可以实现解决 ...
- selenium: where to get ChromeDriver?
address: http://npm.taobao.org/mirrors/chromedriver