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());
}
}
相關文章
- cf900D. Unusual Sequences(容斥 莫比烏斯反演)
- 莫比烏斯反演
- 比較典的莫比烏斯反演
- Hackerrank GCD Product(莫比烏斯反演)GC
- 莫比烏斯反演學習筆記筆記
- Problem H. Curious (莫比烏斯反演)
- 狄利克雷卷積 & 莫比烏斯反演卷積
- 狄利克雷卷積與莫比烏斯反演卷積
- 莫比烏斯
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 演算法隨筆——數論之莫比烏斯反演演算法
- SDOI2018 反迴文串(莫比烏斯反演+Pollard-Rho)
- 莫比烏斯函式函式
- SDOI2018 舊試題(莫比烏斯反演+三元環計數)
- lg容斥與反演
- 莫比烏斯函式 - 學習筆記函式筆記
- 容斥
- 反射容斥反射
- 容斥原理
- 【模板】容斥原理
- Min-Max 容斥
- 容斥原理學習筆記筆記
- 容斥定理 AtCoder——FizzBuzz Sum Hard
- 容斥原理——數學知識
- MMBT3904-ASEMI低壓NPN三極體MMBT3904
- P4178 Tree——點分治 容斥
- P1447 [NOI2010] 容斥原理
- Min-Max 容斥學習筆記筆記
- CodeForces571A. Lengthening Sticks(組合數學-容斥)
- 2018世界盃烏拉圭vs俄羅斯誰會贏 烏拉圭vs俄羅斯比分預測
- HDU 4135 Co-prime(容斥原理+分解質因數)
- 薩繆爾·莫爾斯:電報之父的傳奇人生
- cf449D. Jzzhu and Numbers(容斥原理 高維字首和)
- 機率期望進階 + Min-Max容斥 練習題
- 「數學」助力每一個不知死活的容斥夢
- 《漫威蜘蛛俠:邁爾斯·莫拉萊斯》IGN評測:擺盪到新高度
- codeforces Photo of The Sky
- Ignatius and the Princess IV——伊格納提烏斯和四公主
- 人工智慧原理期末速成——消解反演與反演求解人工智慧