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

 #include<stdio.h>
#include<string.h>
#include<math.h>
int F[ + ] , f[ + ];
int num[ + ] ;
int prime[ + ] ;
int a[ + ] ;
int mui[ + ] ;
bool vis[ + ] ;
int b[ + ] ;
int n ;
typedef long long ll ;
void work_miu ()
{
memset (prime , , sizeof(prime)) ;
memset (mui , , sizeof(mui)) ;
memset (vis , , sizeof(vis)) ;
for (int i = ; i <= ; i ++) a[i] = i ;
for (int i = ; i <= ; i ++) {
for (int j = i ; j <= ; j += i ) {
if (a[j] % i == && ! vis[j] ) {
int cnt = ;
while (a[j] % i == ) {
a[j] /= i ;
cnt ++ ;
}
if (cnt > ) { vis[j] = ; mui[j] = ;}
else mui[j] ++ ;
}
}
}
/* printf ("μ_source:\n") ;
for (int i = 2 ; i <= 5 ; i ++) printf ("ID %d: %d\n" , i , mui[i]) ; puts (""); */
mui[] = ;
for (int i = ; i <= ; i++) {
if ( mui[i] ) mui[i] = (int) pow (- , mui[i]) ;
}
} void init() {
mui[] = ;
for (int i = ; i <= ; ++ i) {
int x = i;
int cnt = ;
bool fuck = false;
for (int j = ; j * j <= i; ++ j) {
if (x % j == ) {
x /= j;
cnt ++;
if (x % j == ) {
fuck = true;
break;
}
}
}
if (x != ) cnt ++;
mui[i] = (cnt & ) ? - : ;
if (fuck) mui[i] = ;
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
work_miu () ;
//init();
while (~ scanf ("%d", &n) ) {
int x ;
memset (F , , sizeof(F)) ;
memset (num , , sizeof(num) ) ;
memset (f , , sizeof(f)) ;
for (int i = ; i < n ; i ++) {
scanf ("%d" , &b[i]) ;
num[ b[i] ] ++ ;
}
ll sum = ;
for (int i = ; i >= ; i --) {
int cnt = ;
for (int j = i ; j <= ; j += i) {
cnt += num[j] ;
}
if (cnt > ) {
F[i] = (cnt * cnt - cnt) / ;
for (int j = i ; j <= ; j += i ) {
f[i] += mui[j / i] * F[j] ;
}
/* if (f[i] != 0) {
printf ("ID %d : %d\n" , i , f[i]) ;
}*/
sum += 1ll * f[i] * (i * i - i ) ;
}
} //puts ("") ;
/* puts ("F(X) :") ;
for (int i = 1 ; i <= 5 ; i ++) printf ("ID %d: %d \n" , i , F[i]) ; puts ("") ;
printf ("μ:\n") ;
for (int i = 1 ; i <= 5 ; i ++) printf ("ID %d: %d\n" , i , mui[i]) ; puts ("") ;
printf ("sum=%d\n" , sum ) ; */
sum *= ;
for (int i = ; i < n ; i ++) {
sum += b[i] * b[i] - b[i] ;
}
printf ("%I64d\n" , sum % ) ;
}
return ;
}

复杂度这里,因为用的是埃帅,所以为O(nlog(log(n)) ) .

我从杰哥那里学到了一种和百度上不同的莫比乌斯反演写法(个人感觉不错):

n为d的所有倍数。

则:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWcAAAA3CAIAAABfIxyZAAAK00lEQVR4nO2czWsbSRrG51+oW4EYhKB3QAhjhA3CiGCBiFiiIYgQaCyICLjJQRh7sCAGL+gwzoKGaQaFZQxig33RjnFGhyZR2GgRZAMjhp14V4yCSQa0YMWEhkAzsOjQt9pD66vVLan6U1/v7zRhWu56+n3r6aq3qvozAgAAYITPpt0AAADmDHANAACMAa4BAIAxwDUAADAGuAYAAMYA1wAAwBjgGgAAGANcAwAAY4BrAI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dA6JtB4vvGq1CHOF0WTlBhG/+6SARyQ+uodSyAeTZfdn5Ml0in0960+VF2cS3dIyIds81INp2sPiuQURhd9XPZvZjseiGh/Gv3T1pkr5rfDxJIE+qJBEiVw/8mFmNbp1AhWx+0Y921zUg2rawBK6hD6y8LhOw8mor4BrAEgCuYSvgGsASAK5hK0vrGgAAmARcAwAAY4BrAABgjP8DcZJ3UH+Jf3oAAAAASUVORK5CYII=" alt="" />

μ(1) = 1 ;

k = p1 * p2 * p3 ……*pr(k由r个不同的质数组成)则μ(k) = -1^k ;

其他情况,μ (k) = 0 ;

这道题F(x)指的是x的倍数的对数的个数有多少。

f(x) = 最大公约数为x的多数有多少。

比如:

F(1) = f(1) + f(2) + f(3) + f(4) = 7 + 2 + 0 + 1 = 10

得到F(x)是非常容易的可以统计x的倍数有多少个,比如说=cnt ;

那么此时的F(x) = (cnt * cnt - cnt) / 2 ;(稍微想想就能想通)

Then , it's the time for 。。。。。

hdu.5212.Code(莫比乌斯反演 && 埃氏筛)的更多相关文章

  1. HDU 5212 Code (莫比乌斯反演)

    题意:给定上一个数组,求 析: 其中,f(d)表示的是gcd==d的个数,然后用莫比乌斯反演即可求得,len[i]表示能整队 i 的个数,可以线性筛选得到, 代码如下: #pragma comment ...

  2. hdu 6134 Battlestation Operational (莫比乌斯反演+埃式筛)

    Problem Description   > The Death Star, known officially as the DS-1 Orbital Battle Station, also ...

  3. [HDU 5608]Function(莫比乌斯反演 + 杜教筛)

    题目描述 有N2−3N+2=∑d∣Nf(d)N^2-3N+2=\sum_{d|N} f(d)N2−3N+2=∑d∣N​f(d) 求∑i=1Nf(i)\sum_{i=1}^{N} f(i)∑i=1N​f ...

  4. [复习]莫比乌斯反演,杜教筛,min_25筛

    [复习]莫比乌斯反演,杜教筛,min_25筛 莫比乌斯反演 做题的时候的常用形式: \[\begin{aligned}g(n)&=\sum_{n|d}f(d)\\f(n)&=\sum_ ...

  5. [JXOI 2018] 游戏 解题报告 (组合数+埃氏筛)

    interlinkage: https://www.luogu.org/problemnew/show/P4562 description: solution: 注意到$l=1$的时候,$t(p)$就 ...

  6. CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)

    Recently, the bear started studying data structures and faced the following problem. You are given a ...

  7. cf1154G 埃氏筛应用

    直接用埃氏筛也可以做,但是这题写起来有点恶臭.. 更加简单的写法是直接枚举gcd=k,然后里面再枚举一次i*k,即找到k两个最小的倍数,看起来复杂度很高,但其实也是埃氏筛的复杂度 因为每次枚举gcd, ...

  8. POJ3094 Sky Code(莫比乌斯反演)

    POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使 ...

  9. 【bzoj3930】[CQOI2015]选数 莫比乌斯反演+杜教筛

    题目描述 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公约数,以便进一 ...

随机推荐

  1. 【Phylab2.0】Alpha版本发布说明

    新版本特性 更多的实验 Part Phylab2.0[Alpha] Phylab1.0 1011 1010113 缺失 1011 1010212 缺失 1021 1020113 1021 102021 ...

  2. Linux 中 17 个 tar 命令实用示例

    Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...

  3. Docker入门教程(二)命令

    Docker入门教程(二)命令 [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第二篇,介绍了Docker的基本命令以及命令的用法和功能. 在Docker ...

  4. python调用ggsci.exe程序

    需求:通过python调用windows server 2008下的ogg同步程序,实现图形化控制. 简单GUI

  5. webView(简单的浏览器)

    #import "MJViewController.h" @interface MJViewController () <UISearchBarDelegate, UIWeb ...

  6. JNI笔记

    由于要做一个能够加红字体的dialog,而cocos2d中的CCMessageBox是系统内带的,我无法修改其字体颜色.事实上是可以修改的,通过观察发现CCMessageBox被调用后,在安卓平台中会 ...

  7. 20145212《Java程序程序设计》课程总结

    20145212<Java程序程序设计>课程总结 一.每周读书笔记链接汇总 第一周读书笔记 第二周读书笔记 第三周读书笔记 第四周读书笔记 第五周读书笔记 第六周读书笔记 第七周读书笔记 ...

  8. Java——布局管理器

    在Swing中使用的所有布局管理器都可以实现LayoutManager接口,在Swing中主要使用的5种布局管理器:FlowLayout.BorderLayout.GridLayout.CardLay ...

  9. 一个 Sql语句优化的问题- STATISTICS 统计信息

    前段时间,同事遇到一个 Sql语句的问题,一个列表分页功能响应在30 s以上,看数据库里面的数据条数,数据量也不大,相关字段的一些索引也都有,可就是慢.于是找出具体的sql 语句出来分析,分页功能主要 ...

  10. xcode

    1: info.plist IOS之Info.plist文件简介 http://www.apkbus.com/android-130240-1-1.html 在Xcode项目中读取plist文件 ht ...