Code( BestCoder Round #39 ($) C) (莫比乌斯反演)

Code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 209    Accepted Submission(s): 85

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;

}

Input

There are Multiple Cases.(At MOST
10)

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).

Output

For each case:

Print an integer,denoting what the function returns.

Sample Input

5
1 3 4 2 4

Sample Output

64

Hint

gcd(x,y) means the greatest common divisor of x and y.

 

Source

BestCoder Round #39 ($)

题意: 简单易懂,就给你一段代码,叫你优化;

题解: 莫比乌斯反演, 首先我们设f(d)表示在给出的所有数中有f(d)对最大公约数是d. cnt(n) 表示在给出的所有数中有cnt(n)个是n的倍数(包含n). 假设我们已经知道了这两个函数,然后就可以用莫比乌斯反演做了. 首先F(n) = cnt(n) * cnt(n); 表示大于等于n的所有数组成的对数,那么有F(n) = f(n) + f(n * 2) + f(n * 3) + .....f(max);

做完以上步骤,就可以套用莫比乌斯反演做了.

莫比无私反演的公式在bin神的博客上有,去搬吧! 啊啊啊啊啊啊啊啊啊啊啊!!!!!!!!!!!!

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int MAXN = 1e4;
typedef long long ll;
const int mod = 10007;
bool check[MAXN + 10];
ll prime[MAXN + 10],cnt[MAXN + 10];
ll mu[MAXN + 10],F[MAXN + 10];

void Moblus()
{
    memset(check,false,sizeof(check));
    mu[1] = 1;
    int tot = 0;
    for(int i = 2; i <= MAXN; i++)
    {
        if(!check[i])
        {
            prime[tot++] = i;
            mu[i] = -1;
        }
        for(int j = 0; i * prime[j] <= MAXN; j++)
        {
            check[i * prime[j]] = true;
            if(i % prime[j] == 0)
            {
                mu[i * prime[j]] = 0;
                break;
            }
            mu[i * prime[j]] = -mu[i];
        }
    }
}

int main()
{
    //freopen("in","r",stdin);
    Moblus();
    int n,x;
    while(~scanf("%d",&n))
    {
        memset(cnt,0,sizeof(cnt));
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&x);
            cnt[x]++;
        }
        for(int i = 1; i <= MAXN; i++)
            for(int j = i * 2; j <= MAXN; j += i)
                cnt[i] += cnt[j];
        for(int i = 1; i <= MAXN; i++) F[i] = cnt[i] * cnt[i];
        ll res = 0;
        for(int i = 1; i <= MAXN; i++)
        {
            ll tp = 0;
            for(int j = i; j <= MAXN; j += i)
            {
                tp += mu[j / i] * F[j];
                if(tp >= mod) tp %= mod;
            }
            res += tp * i % mod * (i - 1);
            if(res >= mod) res %= mod;
        }
        printf("%I64d\n",res);
    }
    return 0;
}
时间: 2024-10-14 04:42:06

Code( BestCoder Round #39 ($) C) (莫比乌斯反演)的相关文章

HDU 5212 Code(容斥 或 莫比乌斯反演)

Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 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 ve

hdu.5212.Code(莫比乌斯反演 &amp;&amp; 线性筛)

Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 300    Accepted Submission(s): 124 Problem Description WLD likes playing with codes.One day he is writing a function.Howerver,his computer b

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 ste

POJ3094 Sky Code(莫比乌斯反演)

POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使得gcd(a,b,c,d)=1的四元组有多少? 解法 枚举一个约数\(k\),看看总共有多少个数\(S_k=\{x\}\)满足\(k|x\).那么可以保证(a,b,c,d)有的一个共同的因子是k,这样的四元组的个数就是 \[ F(k)={|S_k|\choose 4} \] 这样算会算重,比如枚举到

BZOJ 1114 Number theory(莫比乌斯反演+预处理)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=71738 题意:给你一个整数序列a1, a2, a3, ... , an.求gcd(ai, aj) = 1 且 i < j的对数. 思路:利用莫比乌斯反演很快就能得到公式,但是求解时我们要知道序列中1, 2, 3, ... , max(a1, a2, ... , an)的倍数各是多少.我们用num[i]=k,来表示序列中有k个数是i的倍数,那么这部分对结果的影响是m

bzoj 1101 [POI2007]Zap - 莫比乌斯反演

Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a ,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到你的帮助. Input 第一行包含一个正整数n,表示一共有n组询问.(1<=n<= 50000)接下来n行,每行表示一个询问,每行三个 正整数,分别为a,b,d.(1<=d<=a,b<=50000) Output 对于每组询问,输出到输出文件zap.out一个正

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin

莫比乌斯反演 - HNU 13412 Cookie Counter

Cookie Counter Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13412&courseid=0 Mean: 将N分为D份,每份不超过X,有多少种分法? analyse: 莫比乌斯反演的运用. 首先我们想到的是迭代,但是数据太大,一路迭代下去必定爆栈+超内存+TLE. 那么就需要用莫比乌斯反演来优化多项式求和.我们枚举X,对于满足条件的X,使用莫比乌斯反演求和统计答案,不满足条

【莫比乌斯反演】BZOJ2005 [NOI2010]能量采集

Description 求sigma gcd(x,y)*2-1,1<=x<=n, 1<=y<=m.n, m<=1e5. Solution f(n)为gcd正好是n的(x,y)的个数 F(n)为gcd是n的倍数的(x,y)的个数 我们要求的就是f(i) 然而这个不好直接算,可F(i)可以直接用(n/i)*(m/i)得到 那么有F(n)=sigma n|i f(i) 于是有f(n)=sigma n|i mu(i)*F(i) 这就是莫比乌斯反演,不过这道题直接用容斥的思想想也很容易