HDU 5212 Code (容斥 莫比烏斯反演基礎題)
Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 519 Accepted Submission(s): 206
Problem Description
WLD likes playing with codes.One day he is writing a function.Howerver,his computer breaks down because the function is too powerful.He is very sad.Can you help him?
The function:
int calc
{
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
res%=10007;
}
return res;
}
The function:
int calc
{
int res=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
res+=gcd(a[i],a[j])*(gcd(a[i],a[j])-1);
res%=10007;
}
return res;
}
There are Multiple Cases.(At MOST10)
For each case:
The first line contains an integer N(1≤N≤10000).
The next line contains N integers a1,a2,...,aN(1≤ai≤10000).
For each case:
The first line contains an integer N(1≤N≤10000).
The next line contains N integers a1,a2,...,aN(1≤ai≤10000).
For each case:
Print an integer,denoting what the function returns.
Print an integer,denoting what the function returns.
5
1 3 4 2 4
64
Hint
gcd(x,y) means the greatest common divisor of x and y.BestCoder Round #39 ($)
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5212
題目大意:就是求那個程式的值
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5212
題目大意:就是求那個程式的值
題目分析:顯然n方會炸,因為ai最大才1e4,可以列舉gcd的值,但是計算的時候會重複,要容斥一下,這裡直接用莫比烏斯反演來容斥了
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 10005;
int const MOD = 10007;
int mob[MAX], p[MAX], num[MAX], cnt[MAX]; //num表示i的倍數的個數,cnt表示i的個數
bool noprime[MAX];
int ma, n;
void Mobius()
{
int pnum = 0;
mob[1] = 1;
for(int i = 2; i < MAX; i++)
{
if(!noprime[i])
{
p[pnum ++] = i;
mob[i] = -1;
}
for(int j = 0; j < pnum && i * p[j] < MAX; j++)
{
noprime[i * p[j]] = true;
if(i % p[j] == 0)
{
mob[i * p[j]] = 0;
break;
}
mob[i * p[j]] = -mob[i];
}
}
}
int cal()
{
memset(num, 0, sizeof(num));
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++)
{
for(int j = i; j <= ma; j += i)
{
ll tmp = (ll) (num[j] * num[j] % MOD); //j的倍數的二元組數
ans = (ans % MOD + (ll) mob[j / i] * tmp * i * (i - 1) % MOD + MOD) % MOD; //容斥
}
}
return ans;
}
int main()
{
Mobius();
while(scanf("%d", &n) != EOF)
{
memset(cnt, 0, sizeof(cnt));
int tmp;
ma = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &tmp);
cnt[tmp] ++;
ma = max(ma, tmp);
}
printf("%d\n", cal());
}
}
相關文章
- cf900D. Unusual Sequences(容斥 莫比烏斯反演)
- 莫比烏斯反演
- 比較典的莫比烏斯反演
- Hackerrank GCD Product(莫比烏斯反演)GC
- 莫比烏斯反演學習筆記筆記
- Problem H. Curious (莫比烏斯反演)
- 狄利克雷卷積 & 莫比烏斯反演卷積
- 狄利克雷卷積與莫比烏斯反演卷積
- 莫比烏斯
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 演算法隨筆——數論之莫比烏斯反演演算法
- SDOI2018 舊試題(莫比烏斯反演+三元環計數)
- SDOI2018 反迴文串(莫比烏斯反演+Pollard-Rho)
- 莫比烏斯函式函式
- lg容斥與反演
- 莫比烏斯函式 - 學習筆記函式筆記
- HDU 4135 Co-prime(容斥原理+分解質因數)
- 容斥
- HDU 4135——Co-prime(容斥原理&&二進位制列舉)
- 基礎莫隊模板
- 反射容斥反射
- 容斥原理
- 【莫煩】python基礎教程Python
- 【模板】容斥原理
- 基礎莫隊演算法演算法
- Min-Max 容斥
- BUUCTF 基礎CODE REVIEWView
- 容斥原理學習筆記筆記
- 容斥定理 AtCoder——FizzBuzz Sum Hard
- 容斥原理——數學知識
- 機率期望進階 + Min-Max容斥 練習題
- Terraform基礎入門 (Infrastructure as Code)ORMASTStruct
- P4178 Tree——點分治 容斥
- python基礎內容Python
- [kuangbin帶你飛]專題十二 基礎DP1 D - Doing Homework HDU - 1074
- P1447 [NOI2010] 容斥原理
- Min-Max 容斥學習筆記筆記
- 「暑期訓練」「基礎DP」 Monkey and Banana (HDU-1069)NaN
- 逆向基礎 Finding important/interesting stuff in the code (二)ImportREST