hdu.5212.Code(莫比乌斯反演 && 埃氏筛)
Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 300 Accepted Submission(s): 124
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;
}
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).
Print an integer,denoting what the function returns.
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+n3r6aq3qvozAgAAYITPpt0AAADmDHANAACMAa4BAIAxwDUAADAGuAYAAMYA1wAAwBjgGgAAGANcAwAAY4BrAIDtyPX8rTU/g/13C69eHMTWYuydde/qVqEuT7tltgCuAQB20zyOxR6/v8iFEEI4krtoEyKXUgghTph202wBXAMAbObj3+5Hc7VmIY4Qij1+LxNCiFRkEUKp0kIMNsA1AMAJFJsI8w1CCCGywGGEwrnFmKIsqmu8y0d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mor4BrAEgCuYSvgGsASAK5hK0vrGgAAmARcAwAAY4BrAABgjP8DcZJ3UH+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(莫比乌斯反演 && 埃氏筛)的更多相关文章
- HDU 5212 Code (莫比乌斯反演)
题意:给定上一个数组,求 析: 其中,f(d)表示的是gcd==d的个数,然后用莫比乌斯反演即可求得,len[i]表示能整队 i 的个数,可以线性筛选得到, 代码如下: #pragma comment ...
- hdu 6134 Battlestation Operational (莫比乌斯反演+埃式筛)
Problem Description > The Death Star, known officially as the DS-1 Orbital Battle Station, also ...
- [HDU 5608]Function(莫比乌斯反演 + 杜教筛)
题目描述 有N2−3N+2=∑d∣Nf(d)N^2-3N+2=\sum_{d|N} f(d)N2−3N+2=∑d∣Nf(d) 求∑i=1Nf(i)\sum_{i=1}^{N} f(i)∑i=1Nf ...
- [复习]莫比乌斯反演,杜教筛,min_25筛
[复习]莫比乌斯反演,杜教筛,min_25筛 莫比乌斯反演 做题的时候的常用形式: \[\begin{aligned}g(n)&=\sum_{n|d}f(d)\\f(n)&=\sum_ ...
- [JXOI 2018] 游戏 解题报告 (组合数+埃氏筛)
interlinkage: https://www.luogu.org/problemnew/show/P4562 description: solution: 注意到$l=1$的时候,$t(p)$就 ...
- CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)
Recently, the bear started studying data structures and faced the following problem. You are given a ...
- cf1154G 埃氏筛应用
直接用埃氏筛也可以做,但是这题写起来有点恶臭.. 更加简单的写法是直接枚举gcd=k,然后里面再枚举一次i*k,即找到k两个最小的倍数,看起来复杂度很高,但其实也是埃氏筛的复杂度 因为每次枚举gcd, ...
- POJ3094 Sky Code(莫比乌斯反演)
POJ3094 Sky Code(莫比乌斯反演) Sky Code 题意 给你\(n\le 10^5\)个数,这些数\(\le 10^5\),问这些这些数组成的互不相同的无序四元组(a,b,c,d)使 ...
- 【bzoj3930】[CQOI2015]选数 莫比乌斯反演+杜教筛
题目描述 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公约数,以便进一 ...
随机推荐
- Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found
今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...
- 事务块TransactionScope使用
TransactionScope 可以让代码块成为事务性代码块. 当发生异常时,会自动回滚.后期手动提交事务. 简单的例子: using (TransactionScope ts = new Tran ...
- 重写Object类中的equals方法
Object是所有类的父亲,这个类有很多方法,我们都可以直接调用,但有些方法并不适合,例如下面的student类 public class Student { //姓名.学号.年纪 private S ...
- python调用ggsci.exe程序
需求:通过python调用windows server 2008下的ogg同步程序,实现图形化控制. 简单GUI
- 任务二:1、选择器 2、连接集中状态的顺序 3、浮动的用发和原理 4、盒模型在IE和Google等不同浏览器的区别与联系
1.选择器类型 标签选择器,类选择器,ID选择器,组合选择器,伪类和伪元素,属性选择器,子选择器,同胞选择器, :not()选择器 1.同胞选择器 相邻同胞标签: h2 + p{ color:red; ...
- CentOS7安装Ambari
环境: CentOS7安装两个节点:master.slave1.并配置ssh无密码登录. 步骤: 获取 Ambari 的公共库文件(public repository): wget http://pu ...
- JS定时器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- yourphp的edit,updata,dele
参考文件Yourphp\Lib\Action\User\PostAction.class.php public function add() { $form=new Form(); $form-> ...
- Intent启动一个新的页面
一,Intent(目的) 的分类 显式 Intent 构造函数重载之一: Intent intent = new Intent(FirstActivity.this,SecondActivity.cl ...
- MySQL编译安装错误:No curses/termcap library found的解决方法
CentOS编译安装MySQL,./coonfigure时出现错误: checking for termcap functions library... configure: error: No cu ...