博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3904 Sky Code
阅读量:2442 次
发布时间:2019-05-10

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

Sky Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1694   Accepted: 523

Description

Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing to steal the spacecraft of Petru. There is only one problem – Petru has locked the spacecraft with a sophisticated cryptosystem based on the ID numbers of the stars from the Milky Way Galaxy. For breaking the system Stancu has to check each subset of four stars such that the only common divisor of their numbers is 1. Nasty, isn’t it? Fortunately, Stancu has succeeded to limit the number of the interesting stars to N but, any way, the possible subsets of four stars can be too many. Help him to find their number and to decide if there is a chance to break the system.

Input

In the input file several test cases are given. For each test case on the first line the number N of interesting stars is given (1 ≤ N ≤ 10000). The second line of the test case contains the list of ID numbers of the interesting stars, separated by spaces. Each ID is a positive integer which is no greater than 10000. The input data terminate with the end of file.

Output

For each test case the program should print one line with the number of subsets with the asked property.

Sample Input

42 3 4 5 42 4 6 8 72 3 4 5 7 6 8

Sample Output

1 0 34

题意:给定n个数a1,a2,a3,…,an,从中选出四个数,使得他们之间的最大公约数为1,问有多少种取法?

解题思路:对每一个数ai,我们对它进行质因数分解,然后利用2^k算法找出所有由质因数组成的数,并记录组成该数的质因数个数,最后运用容斥原理进行求解;

参考代码:

#include 
#include
using namespace std;#define MAX_N 10000+5#define MAX_FACTOR 16typedef long long ll;int n,f[MAX_N],count[MAX_N],factor[MAX_FACTOR],factor_num[MAX_N];void solve(int a){ int k=0; for (int i=2;i*i<=a;i++){ //质因数分解 if (a%i==0){ factor[k++]=i; while (a%i==0) a/=i; } } if (a>1) factor[k++]=a; for (int i=1;i<(1<
>n){ for (int i=0;i
>f[i]; memset(count,0,sizeof(count)); memset(factor_num,0,sizeof(factor_num)); for (int i=0;i

转载地址:http://rfbqb.baihongyu.com/

你可能感兴趣的文章
Verizon选择Borland控制开发流程并降低风险
查看>>
Borland 崭新的Caliber Define IT产品
查看>>
IBM Rational RequisitePro集成简介
查看>>
OOAD利器Rational Rose的介绍
查看>>
一年的测试生活和感悟
查看>>
通过RUP用例进行需求管理的可追踪性策略(2)
查看>>
持续改进之配置管理变更的关键路径
查看>>
postgresql 优化与维护
查看>>
mongodb replica sets 测试
查看>>
linux AS6.2 与 as5.4 的对比,性能提升明显
查看>>
FLASHCACHE 的是是非非
查看>>
length() between oracle and postgresql
查看>>
99-lisp lisp 的99个问题 P1-10
查看>>
PG 函数的易变性(Function Volatility Categories)
查看>>
Lisp Quote 和Backquote分析
查看>>
PG psql 变彩色显示
查看>>
SICP 练习 1.3
查看>>
pg 数据库HA 启动脚本的两个假设
查看>>
sql_log_bin在GTID复制下的一个现象
查看>>
双主+haproxy手工切换的一个注意点
查看>>