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 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 ($)

先占坑,晚点补莫比乌斯

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e4+10,M=1e6+10,inf=1e9+10;
const LL INF=1e18+10,mod=1e9+7;

int cnt[N],sum[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(cnt,0,sizeof(cnt));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            cnt[x]++;
        }
        for(int i=1;i<=10000;i++)
        {
            for(int j=i;j<=10000;j+=i)
                sum[i]+=cnt[j];
            sum[i]=sum[i]*sum[i];
        }
        LL ans=0;
        for(int i=10000;i>=1;i--)
        {
            for(int j=i+i;j<=10000;j+=i)
                sum[i]-=sum[j];
            //if(sum[i])cout<<i<<" "<<sum[i]<<endl;
            ans+=1LL*i*(i-1)*sum[i];
            ans%=10007;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-13 23:08:05

hdu 5212 Code 筛法或者莫比乌斯的相关文章

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

莫比乌斯二连 HDU 5212 Code &amp; HDU 1695 GCD

莫比乌斯的模板题 都是差不多的 F(m)为gcd(i,j) = m(i∈[1,m],j∈[1,n])的个数 f(m) = ∑(m\d) F(d)  意义为gcd(i,j)为m的倍数的个数 运用莫比乌斯反演得到 F(m) = ∑(m\d)μ(d/m) * f(d) #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

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 2017 Code Lock (并查集的应用+快速幂)

链接:HDU 3461 题目大意: 题目的大意是一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限次的"增加"操作后可以变成另一组密码,那么我们认为这两组密码是相同的.该题的目标就是在给定N.M和M个区间的前提下计算有多少种不同的密码. 根据题意,如果一个可调整的区间都没有的话,答案应该是26

HDU 3461 Code Lock(并查集的应用+快速幂)

* 65536kb,只能开到1.76*10^7大小的数组.而题目的N取到了10^7,我开始做的时候没注意,用了按秩合并,uset+rank达到了2*10^7所以MLE,所以貌似不能用按秩合并. 其实路径压缩也可以不用.............  题目的大意: 一个密码锁上有编号为1到N的N个字母,每个字母可以取26个小写英文字母中的一个.再给你M个区间[L,M],表示该区间的字母可以一起同步"增加"(从'a'变为'b'为增1,'z'增1为'a').假如一组密码按照给定的区间进行有限

Code HDU 5212

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

hdu 5212 反向容斥或者莫比

http://acm.hdu.edu.cn/showproblem.php?pid=5212 题意:忽略.. 题解:把题目转化为求每个gcd的贡献.(http://www.cnblogs.com/z1141000271/p/7419717.html 和这题类似 反向容斥)这里先用容斥写了,mobious的之后再说吧23333. 然后比较想说的是这个调和级数的复杂度 nlog(n) ac代码: #include <iostream> #include <cstdio> #includ

HDU 3461 Code Lock(并查集+二分求幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3461 A lock you use has a code system to be opened instead of a key. The lock contains a sequence of wheels. Each wheel has the 26 letters of the English alphabet 'a' through 'z', in order. If you move a

hdu 5317 RGCDQ 筛法+线段树解法

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 299    Accepted Submission(s): 151 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and mor