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());
}
}
相關文章
- HDU 1695 GCD (容斥 + 莫比烏斯反演)GC
- POJ 3904 Sky Code (容斥+莫比烏斯反演)
- Codeforces 548E Mike and Foam (容斥+莫比烏斯反演)
- ZOJ 3868 GCD Expectation (容斥+莫比烏斯反演)GC
- lg容斥與反演
- SPOJ VLATTICE Visible Lattice Points (莫比烏斯反演基礎題)
- ZOJ 3435 Ideal Puzzle Bobble (莫比烏斯反演基礎題)Idea
- HDU 4746 Mophues (莫比烏斯反演應用)
- 莫比烏斯反演
- HDU 4059 The Boss on Mars ( 容斥原理)
- HDU4390Number Sequence(容斥原理)
- HDU4407Sum ( 容斥原理)
- HDU 5468 Puzzled Elena(DFS序+容斥原理)
- BZOJ 2301 [HAOI2011]Problem b (容斥+莫比烏斯反演+分塊優化 詳解)優化
- HDU2841 Visible Trees (容斥原理)
- Hackerrank GCD Product(莫比烏斯反演)GC
- HDU 1695-GCD(容斥原理+尤拉函式)GC函式
- 莫比烏斯反演學習筆記筆記
- 比較典的莫比烏斯反演
- Codeforces 235E Number Challenge (神定理+莫比烏斯反演)
- 容斥定理 AtCoder——FizzBuzz Sum Hard
- 有標號DAG計數 [容斥原理 子集反演 組合數學 fft]FFT
- HDU 5072 Coprime (單色三角形問題+容斥原理)
- 容斥
- HDU 4135 Co-prime(容斥原理+分解質因數)
- 狄利克雷卷積 & 莫比烏斯反演卷積
- 反射容斥反射
- 容斥原理
- 狄利克雷卷積與莫比烏斯反演卷積
- SPOJ PGCD - Primes in GCD Table (好題! 莫比烏斯反演+分塊求和優化)GC優化
- HDU 4135——Co-prime(容斥原理&&二進位制列舉)
- BZOJ 3309 DZY Loves Math (莫比烏斯反演的應用 好題)
- BZOJ 2818 Gcd (莫比烏斯反演 或 尤拉函式)GC函式
- 演算法隨筆——數論之莫比烏斯反演演算法
- 【模板】容斥原理
- 洛谷 P2257 YY的GCD(莫比烏斯反演)GC
- 容斥原理講解
- Min-Max 容斥