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/qVqEuT7tltgCuAQB20zyO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Ml0in0960+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(莫比乌斯反演 && 埃氏筛)的更多相关文章
- 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个整数都求一次最大公约数,以便进一 ...
随机推荐
- tomcat 和servlet之间的关系
http://tomcat.apache.org/whichversion.html pache Tomcat Versions Apache Tomcat® is an open source so ...
- Why TestNG?
最近计划将工程中的单元测试从JUnit迁移到TestNG上面. Why TestNG(http://kaczanowscy.pl/tomek/sites/default/files/testng_vs ...
- linux(centos) 项目部署阶段相关命令汇总
1.ssh免密码登陆主要命令cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys-->添加公钥service sshd restart -- ...
- JS生成随机数的各种函数
第一种方法 /* *@desc:生成随机字符串 *@remark:toString方法可以接收一个基数作为参数的原理,这个基数从2到36封顶.如果不指定,默认基数是10进制 */ function g ...
- rabbitmq因为主机名更改导致连接不上
今天创建rabbitmq用户的时候报错,说nodedown,定睛一看,发现主机名是localhost,而此时我们的主机名是controller01,所以猜测是主机名导致的,重启服务器,就成功添加了. ...
- 一文彻底了解join的各种用法
表a 表b a1 a2 b1 b2 a01 张三 a02 数学 a02 ...
- FFT质数打表程序
#include<bits/stdc++.h> using namespace std; typedef long long ll; void sol(ll x){ int y=0; fo ...
- js011-DOM扩展
js011-DOM扩展 本章内容 理解Selecters API 使用HTML5 DOM扩展 了解转悠的DOM扩展 11.1选择符API JS中最常用的一项功能,就是根据css选择符选择与某个模式匹配 ...
- 将一个tabel加到另一个table
DataTable rate = GetRate(str_catchType,); dt.Merge(rate);//将两个table合并
- PetaPoco 使用总结(一)
PetaPoco 使用总结(一) 前段时间,公司的一个项目希望用一个ORM 的框架,通过对比 Dapper 和 PetaPoco ,虽然Dapper 功能很强大,速度更快. 但是最终还是选择了比较简单 ...