Sum Of Gcd

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=4676

Description

Given you a sequence of number a1, a2, ..., an, which is a permutation of 1...n.

You need to answer some queries, each with the following format:

Give you two numbers L, R, you should calculate sum of gcd(a[i], a[j]) for every L <= i < j <= R.

Input

First line contains a number T(T <= 10),denote the number of test cases.

Then follow T test cases.

For each test cases,the first line contains a number n(1<=n<= 20000).

The second line contains n number a1,a2,...,an.

The third line contains a number Q(1<=Q<=20000) denoting the number of queries.

Then Q lines follows,each lines contains two integer L,R(1<=L<=R<=n),denote a query.

Output

For each case, first you should print "Case #x:", where x indicates the case number between 1 and T.

Then for each query print the answer in one line.

Sample Input

1

5

3 2 5 4 1

3

1 5

2 4

3 3

Sample Output

Case #1:

11

4

0

Hint

题意

给你n个数,然后Q次询问,每次问你l,r区间的两两之间的GCD和是多少

题解:

莫队+反演,直接暴力莽就好了……

代码

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e4 + 15;

int unit , a[maxn] , N , M , cnt[maxn];
long long ans[maxn] , phi[maxn];
vector < int > factor[maxn]; struct Query{
int l , r , idx;
friend bool operator < (const Query & a , const Query & b){
int x1 = a.l / unit , x2 = b.l / unit;
if( x1 != x2 ) return x1 < x2;
return a.r < b.r;
}
}Q[maxn]; void Init(){
for(int i = 1 ; i < maxn ; ++ i)
for(int j = i ; j < maxn ; j += i)
factor[j].push_back( i );
phi[1] = 1;
for(int i = 2 ; i < maxn ; ++ i)
if( !phi[i] )
for(int j = i ; j < maxn ; j += i){
if( !phi[j] ) phi[j] = j;
phi[j] = phi[j] * ( i - 1 ) / i;
}
} long long add( int x ){
long long res = 0;
for( auto d : factor[x] ) res += cnt[d] * phi[d];
for( auto d : factor[x] ) cnt[d] ++ ;
return res;
} long long del( int x ){
long long res = 0;
for( auto d : factor[x] ) cnt[d] -- ;
for( auto d : factor[x] ) res += cnt[d] * phi[d];
return -res;
} void solve(){
memset( cnt , 0 , sizeof( cnt ) );
int l = 1 , r = 0;
long long cur = 0;
for(int i = 1 ; i <= M ; ++ i){
while( l < Q[i].l ) cur += del( a[l++] );
while( l > Q[i].l ) cur += add( a[--l] );
while( r < Q[i].r ) cur += add( a[++r] );
while( r > Q[i].r ) cur += del( a[r--] );
ans[Q[i].idx] = cur;
}
} int main( int argc , char * argv[] ){
Init();
int Case , cas = 0;
scanf("%d",&Case);
while(Case--){
scanf("%d",&N);
for(int i = 1 ; i <= N ; ++ i) scanf("%d" , a + i);
scanf("%d",&M);
for(int i = 1 ; i <= M ; ++ i){
Q[i].idx = i;
scanf("%d%d",&Q[i].l,&Q[i].r);
}
unit = sqrt( N );
sort( Q + 1 , Q + M + 1 );
solve();
printf("Case #%d:\n" , ++ cas);
for(int i = 1 ; i <= M ; ++ i) printf("%lld\n" , ans[i]);
}
return 0;
}

hdu 4676 Sum Of Gcd 莫队+phi反演的更多相关文章

  1. hdu 4676 Sum Of Gcd 莫队+数论

    题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...

  2. HDU 4676 Sum Of Gcd 【莫队 + 欧拉】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...

  3. hdu 5381 The sum of gcd 莫队+预处理

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  4. HDU-4676 Sum Of Gcd 莫队+欧拉函数

    题意:给定一个11~nn的全排列AA,若干个询问,每次询问给出一个区间[l,r][l,r],要求得出∑l≤i<j≤r  gcd(Ai,Aj)的值. 解法:这题似乎做的人不是很多,蒟蒻当然不会做只 ...

  5. hdu5381 The sum of gcd]莫队算法

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...

  6. Hdu5381-The sum of gcd(莫队)

    题意我就不说了   解析: 莫队,先预处理出以i为右端点的区间的gcd值,有一些连续的区间的gcd值是相同的,比如[j,i],[j+1,i],[j+2,i]的gcd值是相同的,我们可以把[j,j+2] ...

  7. HDOJ 5381 The sum of gcd 莫队算法

    大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...

  8. hdu 4676 Sum Of Gcd

    离线+分块!! 思路:序列a[1],a[2],a[3]……a[n] num[i]表示区间[L,R]中是i的倍数的个数:euler[i]表示i的欧拉函数值. 则区间的GCD之和sum=∑(C(num[i ...

  9. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

随机推荐

  1. python基础===一行 Python 代码实现并行(转)

    原文:https://medium.com/building-things-on-the-internet/40e9b2b36148 译文:https://segmentfault.com/a/119 ...

  2. Linux USB驱动框架分析 【转】

    转自:http://blog.chinaunix.net/uid-11848011-id-96188.html 初次接触与OS相关的设备驱动编写,感觉还挺有意思的,为了不至于忘掉看过的东西,笔记跟总结 ...

  3. JAVA汉字转拼音(取首字母大写)

    import net.sourceforge.pinyin4j.PinyinHelper;import net.sourceforge.pinyin4j.format.HanyuPinyinCaseT ...

  4. python基础-类的起源

    Python中一切事物都是对象. class Foo(object): def __init__(self,name): self.name = name f = Foo("alex&quo ...

  5. liblinear和libsvm区别

    来源于知乎: 1. LibLinear是线性核,LibSVM可以扩展到非线性核(当也能用线性核,但同样在线性核条件下会比LibLinear慢很多).2. 多分类:LibLinear是one vs al ...

  6. IE手工导入证书

    打开cer文件->欢迎使用证书导入向导->下一步->将所有的证书放入下列存储->受信任的根证书颁发机构->完成

  7. Codeforces 429B Working out(递推DP)

    题目链接:http://codeforces.com/problemset/problem/429/B 题目大意:两个人(假设为A,B),打算健身,有N行M列个房间,每个房间能消耗Map[i][j]的 ...

  8. Python 离线环境

    一.应用场景 比如:对于数据安全要求比较严格的机房,服务器是不允许上网的.那么我现在开发了一套python程序,需要一些模块,怎么运行? 二.离线包制作 有2个解决方案: 1. 使用requireme ...

  9. Elasticsearch 6.x 入门测试

    首先听一下官方的话: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 我尝试了使用Java作为Cl ...

  10. Datagridview 中的checkbox 选中或勾选状态失效

    1.问题描述,先选中第一行,再取消选择,然后点击部门全选,第一行没有打钩,状态是不选中的状态. 2.分析代码 先选中第一行,单元格的单击事件中 改变选中状态为1,第一行取消选择,单元格的单击事件中 改 ...