POJ 3904 Sky Code (容斥+莫比烏斯反演)
Sky Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1831 | Accepted: 570 |
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
Southeastern European Regional Programming Contest 2008
題目連結:http://poj.org/problem?id=3904
題目大意:給n個不相同的數,從中任意選出4個,使得它們的最大公約數為1,問有多少種選法
題目分析:首先n小於4肯定是0了,C(n,4) = n * (n - 1) * (n - 2) * (n - 3) / 24,本題資料範圍不大,long long即可,差不多是裸的莫比烏斯反演題了,和NOJ 2079幾乎一樣,ac程式碼在POJ上rank 7
題目連結:http://poj.org/problem?id=3904
題目大意:給n個不相同的數,從中任意選出4個,使得它們的最大公約數為1,問有多少種選法
題目分析:首先n小於4肯定是0了,C(n,4) = n * (n - 1) * (n - 2) * (n - 3) / 24,本題資料範圍不大,long long即可,差不多是裸的莫比烏斯反演題了,和NOJ 2079幾乎一樣,ac程式碼在POJ上rank 7
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e4 + 5;
int mob[MAX], p[MAX], cnt[MAX], num[MAX];
bool prime[MAX];
int n, ma;
void Mobius()
{
memset(prime, true, sizeof(prime));
int pnum = 0;
mob[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(prime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
prime[i * p[j]] = false;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
}
}
ll cal()
{
for(int i = 1; i <= ma; i++)
for(int j = i; j <= ma; j += i)
num[i] += cnt[j];
ll ans = 0;
for(int i = 1; i <= ma; i++)
{
int x = num[i];
if(x >= 4)
ans += (ll) mob[i] * x * (x - 1) * (x - 2) * (x - 3) / 24;
}
return ans;
}
int main()
{
Mobius();
while(scanf("%d", &n) != EOF)
{
ma = 0;
memset(cnt, 0, sizeof(cnt));
memset(num, 0, sizeof(num));
for(int i = 0; i < n; i++)
{
int tmp;
scanf("%d", &tmp);
cnt[tmp] ++;
ma = max(ma, tmp);
}
if(n < 4)
{
printf("0\n");
continue;
}
printf("%lld\n", cal());
}
}
相關文章
- HDU 5212 Code (容斥 莫比烏斯反演基礎題)
- Codeforces 548E Mike and Foam (容斥+莫比烏斯反演)
- ZOJ 3868 GCD Expectation (容斥+莫比烏斯反演)GC
- HDU 1695 GCD (容斥 + 莫比烏斯反演)GC
- lg容斥與反演
- 莫比烏斯反演
- BZOJ 2301 [HAOI2011]Problem b (容斥+莫比烏斯反演+分塊優化 詳解)優化
- Hackerrank GCD Product(莫比烏斯反演)GC
- 莫比烏斯反演學習筆記筆記
- 比較典的莫比烏斯反演
- Codeforces 235E Number Challenge (神定理+莫比烏斯反演)
- 容斥定理 AtCoder——FizzBuzz Sum Hard
- 有標號DAG計數 [容斥原理 子集反演 組合數學 fft]FFT
- 容斥
- 狄利克雷卷積 & 莫比烏斯反演卷積
- HDU 4746 Mophues (莫比烏斯反演應用)
- 反射容斥反射
- 容斥原理
- 狄利克雷卷積與莫比烏斯反演卷積
- POJ 2773 Happy 2006 (二分答案+容斥)APP
- BZOJ 2818 Gcd (莫比烏斯反演 或 尤拉函式)GC函式
- 演算法隨筆——數論之莫比烏斯反演演算法
- 【模板】容斥原理
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 容斥原理講解
- Min-Max 容斥
- CSU 1325 A very hard problem (莫比烏斯反演+分塊求和優化)優化
- SPOJ VLATTICE Visible Lattice Points (莫比烏斯反演基礎題)
- ZOJ 3435 Ideal Puzzle Bobble (莫比烏斯反演基礎題)Idea
- CodeForces571A. Lengthening Sticks(組合數學-容斥)
- 遊戲裡面的容斥原理遊戲
- Codeforces Gym 100548F Color (組合數+容斥)
- SPOJ PGCD - Primes in GCD Table (好題! 莫比烏斯反演+分塊求和優化)GC優化
- 容斥 + 組合數學 ---Codeforces Round #317 A. Lengthening Sticks
- 容斥原理學習筆記筆記
- 容斥原理——數學知識
- #19. 計數(容斥原理)
- HDU 4059 The Boss on Mars ( 容斥原理)