Sky Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2085   Accepted: 665

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

4
2 3 4 5
4
2 4 6 8
7
2 3 4 5 7 6 8

Sample Output

1
0
34
思路:容斥原理;
由于给的数据范围是10000;所以我们先打表10000以内的素数;
然后我们分解每一个数;求出它的各个不同的质因数,然后暴力组合每个数的质因数,在bt数组里记录个数,也就是bt[i],i这个数可以被前面的哪些数整除
最后从1循环到10000,容斥一遍就可以得到不合要求的个数,最后总的减去就行。
由于每个数不过10000,他的质因数不会超过8个,那么复杂度为(n*28);
  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 }
36 while(scanf("%d",&k)!=EOF)
37 {
38 memset(bt,0,sizeof(bt));
39 for(i=0; i<k; i++)
40 {
41 scanf("%d",&aa[i]);
42 }
43 for(i=0; i<k; i++)
44 {
45 int nn=aa[i];
46 int t=0;
47 int flag=0;
48 while(nn>1)
49 {
50 if(flag==0&&nn%ans[t]==0)
51 {
52 flag=1;
53 que.push(ans[t]);
54 nn/=ans[t];
55 }
56 else if(nn%ans[t]==0)
57 {
58 nn/=ans[t];
59 flag=1;
60 }
61 else
62 {
63 flag=0;
64 t++;
65 }
66 }
67 if(nn>1)
68 {
69 que.push(nn);
70 }
71 int xx=0;
72 while(!que.empty())
73 {
74 cc[xx++]=que.front();
75 que.pop();
76 }
77 int x;
78 int y;
79 for(x=1; x<=(1<<xx)-1; x++)
80 {
81 int ak=1;
82 int vv=0;
83 for(j=0; j<xx; j++)
84 {
85 if(x&(1<<j))
86 {
87 vv++;
88 ak*=cc[j];
89 }
90 }
91 bt[ak]+=1;
92 if(vv%2)
93 dd[ak]=true;
94 }
95 }
96 LL sum=0;
97 LL sum1=0;
98 for(i=2; i<=10000; i++)
99 {
100 if(bt[i]>=4)
101 {
102 LL nn=(LL)bt[i]*(LL)(bt[i]-1)*(LL)(bt[i]-2)*(LL)(bt[i]-3)/24;
103 if(dd[i])
104 sum+=nn;
105 else sum-=nn;
106 }
107 }
108 sum1=(LL)k*(LL)(k-1)*(LL)(k-2)*(LL)(k-3)/24;
109 sum1-=sum;
110 printf("%lld\n",sum1);
111 }
112 return 0;
113 }
												

Sky Code(poj3904)的更多相关文章

  1. POJ3904 Sky Code

    题意 Language:Default Sky Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3980 Accepte ...

  2. poj3904 Sky Code —— 唯一分解定理 + 容斥原理 + 组合

    题目链接:http://poj.org/problem?id=3904 Sky Code Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  3. POJ 3904 Sky Code (容斥原理)

    B - Sky Code Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  4. POJ Sky Code 莫比乌斯反演

    N. Sky Code Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB   64-bit integer IO for ...

  5. POJ3094 Sky Code(莫比乌斯反演)

    POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使 ...

  6. Sky Code

    Sky Code 给出n个数,求选出4个数组合,使其gcd为1,,\(n<=10000\),每个数\(<=10000\). 解 理解1:容斥原理 注意到Mobius反演式子不好写出,于是我 ...

  7. POJ3904 Sky Code【容斥原理】

    题目链接: http://poj.org/problem?id=3904 题目大意: 给你N个整数.从这N个数中选择4个数,使得这四个数的公约数为1.求满足条件的 四元组个数. 解题思路: 四个数的公 ...

  8. POJ 3904 Sky Code

    题意:给定n个数ai, ai <= 10000, n <= 10000, 从中选出4个数要求gcd为1,这样的集合有多少个? 分析:首先总共集合nCr(n, 4) = n*(n-1)*(n ...

  9. POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数

    http://poj.org/problem?id=3904   题意:给一些数,求在这些数中找出四个数互质的方案数.   莫比乌斯反演的式子有两种形式http://blog.csdn.net/out ...

随机推荐

  1. Yarn 公平调度器案例

    目录 公平调度器案例 需求 配置多队列的公平调度器 1 修改yarn-site.xml文件,加入以下从参数 2 配置fair-scheduler.xml 3 分发配置文件重启yarn 4 测试提交任务 ...

  2. 日常Java 2021/10/11

    抽象类 所有对象都是通过类描述的,但不是所有的类都是用来描述对象,就好比抽象类,此类中没有足够的信息描述一个对象. 抽象类不能实例化对象,所以抽象类必须的继承,才可以使用. 抽象方法 Abstract ...

  3. 【♪♪♪】网易云音乐mp3真实地址

    参考别人的博客,得到下面的地址,填上ID号即可,后缀的[.mp3]不用输入 http://music.163.com/song/media/outer/url?id= 例如 最终,合并地址为 http ...

  4. Mybatis 批量插入

    一.首先对于批量数据的插入有两种解决方案(下面内容只讨论和Mysql交互的情况) 1)for循环调用Dao中的单条插入方法 2)传一个List<Object>参数,使用Mybatis的批量 ...

  5. Linux基础命令---elinks文本浏览器

    elinks elinks指令是一个纯文本格式的浏览器,支持颜色.表格.鼠标.菜单操作. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法       ...

  6. Linux基础命令---wget下载工具

    wget wget是一个免费的文件下载工具,可以从指定的URL下载文件到本地主机.它支持HTTP和FTP协议,经常用来抓取大量的网页文件. 此命令的适用范围:RedHat.RHEL.Ubuntu.Ce ...

  7. vue项目windows环境初始化

    下载nodejs zip包并加载到环境变量 nodejs的版本最好使用12版,而不是最新版 npm install webpack -gnpm install -g yarnyarn config s ...

  8. Mysql百万级数据索引重新排序

    参考https://blog.csdn.net/pengshuai007/article/details/86021689中思路解决自增id重排 方式一 alter table `table_name ...

  9. 【Java多线程】线程池-ThreadPoolExecutor

    ThreadPoolExecutor提供了四个构造方法: 我们以最后一个构造方法(参数最多的那个),对其参数进行解释: public ThreadPoolExecutor(int corePoolSi ...

  10. sftp 上传下载 命令介绍

    sftp是Secure FileTransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法. sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...