poj3904 Sky Code —— 唯一分解定理 + 容斥原理 + 组合
题目链接:http://poj.org/problem?id=3904
Sky Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2968 | Accepted: 998 |
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
Source
题意:
给出n个数, 随便挑4个, 使得这四个数的最大公约数为1, 问有多少种组合?
题解:
思路:先用容斥原理计算出四个数的最大公约数>=1的组合数, 然后再用总数C(n,4)减之。
1.将每个数进行分解质因数, 然后再根据这些质因数组合出不同的因子,并记录这个因子出现的次数以及由多少个质因数构成。
2.容斥原理:比如因子2的个数为a,则四个数公约数为2的个数 为C(a,4),因子3的个数为b,则四个数公约数为3的个数为C(b,4),因子6(2*3)的个 数为c,则四个数公约数的个数为C(c,4)。 但是公约数为2的情况中或者公约数为3的情况中可能包括公约数为6的情况,相当于几个集合求并集,这就需要容斥定理来做。
3.如果这个因子出现的次数>=4, 则表明这个因子可以作为某四个数的最大公约数的因子。
4.根据容斥原理:当这个因子的由奇数个质因数构成时, 加; 当这个因子由偶数个质因子构成时, 减。
5. ans = C(n,4) - gcd(a,b,c,d)!=1的组合数。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <stack> 11 #include <sstream> 12 #include <algorithm> 13 using namespace std; 14 #define pb push_back 15 #define mp make_pair 16 #define ms(a, b) memset((a), (b), sizeof(a)) 17 #define eps 0.0000001 18 typedef long long LL; 19 const int INF = 2e9; 20 const LL LNF = 9e18; 21 const int mod = 1e9+7; 22 const int maxn = 1e4+10; 23 24 LL pri[maxn], fac_num[maxn], fac_pri[maxn]; 25 LL n, cnt; 26 27 LL C(LL x) 28 { 29 return x*(x-1)*(x-2)*(x-3)/24; 30 } 31 32 void Divide(LL x) 33 { 34 cnt = 0; 35 for(int i = 2; i*i<=x; i++) 36 { 37 if(x%i==0) 38 { 39 pri[cnt++] = i; 40 while(x%i==0) x /= i; 41 } 42 } 43 if(x!=1) pri[cnt++] = x; 44 } 45 46 void Unit() 47 { 48 for(LL s = 1; s < (1<<cnt); s++) //用二进制, 亦可用递归 49 { 50 LL tmp = 1, sum = 0; 51 for(int j = 0; j<cnt; j++) 52 if(s&(1<<j)) 53 { 54 tmp *= pri[j]; 55 sum++; 56 } 57 58 fac_num[tmp]++; 59 fac_pri[tmp] = sum; 60 } 61 } 62 63 void init() 64 { 65 ms(fac_num, 0); 66 ms(fac_pri, 0); 67 68 LL x; 69 for(int i = 1; i<=n; i++) 70 { 71 scanf("%lld",&x); 72 Divide(x); //分解质因数 73 Unit(); //质因数可以组成哪些因子(这些因子就是四个数的约数) 74 } 75 } 76 77 void solve() 78 { 79 LL tmp = 0; 80 for(int i = 1; i<=1e4; i++) //容斥, 计算gcd(a,b,c,d)!=1的个数 81 { 82 if(fac_num[i]>=4) //这个因子的个数必须不小于4, 才能成为4个数的约束 83 { 84 if(fac_pri[i]&1) //素数个数为奇数时, 加 85 tmp += C(fac_num[i]); 86 else //素数个数为偶数时, 减 87 tmp -= C(fac_num[i]); 88 } 89 } 90 LL ans = C(n) - tmp; //总的减去gcd(a,b,c,d)!=1的个数,即为gcd(a,b,c,d)=1的个数。 91 printf("%lld\n", ans); 92 } 93 94 int main() 95 { 96 while(scanf("%lld",&n)!=EOF) 97 { 98 init(); 99 solve(); 100 } 101 }