Code

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1306    Accepted Submission(s): 540

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.

 
 

题意:给定序列1≤i,j≤n,求gcd(a[i],a[j])∗(gcd(a[i],a[j])−1)之和。

思路:倍数莫比乌斯反演。

代码:

 #include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + ;
const int mod=;
int t;
//线性筛法求莫比乌斯函数
bool vis[N + ];
int pri[N + ];
int mu[N + ];
int sum[N]; void mus() {
memset(vis, , sizeof(vis));
mu[] = ;
int tot = ;
for (int i = ; i < N; i++) {
if (!vis[i]) {
pri[tot++] = i;
mu[i] = -;
}
for (int j = ; j < tot && i * pri[j] < N; j++) {
vis[i * pri[j]] = ;
if (i % pri[j] == ) {
mu[i * pri[j]] = ;
break;
}
else mu[i * pri[j]] = -mu[i];
}
}
sum[]=;
for(int i=;i<N;i++) sum[i]=sum[i-]+mu[i];
}
int n,m,k; int a[N];
int b[N];
int F[N];
int main() {
mus();
while(scanf("%d",&n)==){
int ma=;
memset(F,, sizeof(F));
memset(b,, sizeof(b));
for(int i=;i<n;i++){
scanf("%d",&a[i]);
b[a[i]]++;//b[a[i]]的个数
ma=max(ma,a[i]);
}
for(int i=;i<=ma;i++)
for(int j=i;j<=ma;j+=i) F[i]+=b[j];//在范围内i的倍数的个数
ll ans=;
for(int i=;i<=ma;i++){
ll res=;
for(int j=i;j<=ma;j+=i){
if(!F[j]) continue;
res+=1ll*mu[j/i]*F[j]*F[j]%mod;//公约数均为i
}
ans=(ans+res*i%mod*(i-))%mod;//同时乘上i*i-1
}
ans%=mod;
ans+=mod;
ans%=mod;
printf("%lld\n",ans);
}
return ;
}

HDU 5212 莫比乌斯反演的更多相关文章

  1. HDU 4746 (莫比乌斯反演) Mophues

    这道题看巨巨的题解看了好久,好久.. 本文转自hdu4746(莫比乌斯反演) 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<= ...

  2. HDU 1695 (莫比乌斯反演) GCD

    题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是 ...

  3. GCD HDU - 1695 莫比乌斯反演入门

    题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/ ...

  4. hdu 1695(莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. HDU 6053(莫比乌斯反演)

    题意略. 思路:首先想到暴力去扫,这样的复杂度是n * min(ai),对于gcd = p,对答案的贡献应该是 (a1 / p) * (a2 / p) * .... * (an / p),得出这个贡献 ...

  6. hdu 4746Mophues[莫比乌斯反演]

    Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total ...

  7. 算术 HDU - 6715 (莫比乌斯反演)

    大意: 给定$n,m$, 求$\sum\limits_{i=1}^n\sum\limits_{j=1}^m\mu(lcm(i,j))$ 首先有$\mu(lcm(i,j))=\mu(i)\mu(j)\m ...

  8. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

  9. HDU 5382 莫比乌斯反演

    题目大意: 求S(n)的值 n<=1000000 这是官方题解给出的推导过程,orz,按这上面说的来写,就不难了 这里需要思考的就是G(n)这个如何利用积性函数的性质线性筛出来 作为一个质数,那 ...

随机推荐

  1. 关于NativeEvent的处理

    nativeEvent(const QByteArray &eventType, void *message, long *result){ chASSERT(message != NULL) ...

  2. python数据结构(整理)

    http://www.cnblogs.com/yupeng/p/3413763.html 1. 单链表 链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点 ...

  3. 022configparser模块

    #配置模块 #创建import  configparser config  =  configparser.ConfigParser() #添加config["DEFAULT"]  ...

  4. python UI自动化实战记录九:添加日志

    想知道测试脚本运行到了哪一步,在脚本内关键节点处打日志是一个很好的方法.目前只写最简单的方式,logging相关还需要继续深入. 1  引包,并配置info级别以上的都显示 import loggin ...

  5. Intellij IDEA设置注释作者名字

    方法一:File >> Settings >> Editor >>Code Style >> File and Code Templates>&g ...

  6. node express 跨域问题

    express = require('express'); var app = express(); //设置跨域访问 app.all('*', function(req, res, next) { ...

  7. fastcgi c/c++ API 说明

    fastcgi c/c++ API 下载地址:https://github.com/FastCGI-Archives 先上example #include <iostream> #incl ...

  8. 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  9. HDU 1698 【线段树,区间修改 + 维护区间和】

    题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...

  10. Linux学习总结(十)-文件复制及查看, 环境变量

    一 文件复制及移动 1.命令 cp --------copy 的意思格式 cp 选项 源文件 目标文件a: 对于文件我们直接cp 文件 目标文件假定我们在普通用户家目录下/home/lv新建两个普通文 ...