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. java基础79 会话管理(Cookie技术、Session技术)

    1.概念     会话管理:管理浏览器和服务器之间会话过程中产生的会话数据.    Cookie技术:会话数据保存到浏览器客户端.[存 编号/标记(id)]    Session技术:会话技术会保存到 ...

  2. java基础28 单例集合Collection下的List和Set集合

    单例集合体系: ---------| collection  单例集合的根接口--------------| List  如果实现了list接口的集合类,具备的特点:有序,可重复       注:集合 ...

  3. win10 操作配置备忘

    让程序自动启动 如果想要实现应用程序在所有的用户登录系统后都能自动启动,就把该应用程序的快捷方式放到"系统启动文件夹"里: C:\ProgramData\Microsoft\Win ...

  4. 洛谷P2296寻找道路

    传送门啦 题目中有一个条件是路径上的所有点的出边所指向的点都直接或间接与终点连通. 所以我们要先判断能否走这一个点, $ bfs $ 类似 $ spfa $ 的一个判断,打上标记. 在这我反向建图,最 ...

  5. Spark(八)JVM调优以及GC垃圾收集器

    一JVM结构 1 Java内存结构 JVM内存结构主要有三大块:堆内存.方法区和栈. 堆内存是JVM中最大的一块由年轻代和老年代组成,而年轻代内存又被分成三部分,Eden空间.From Survivo ...

  6. python日期格式转换小记

    utc格林时间==>东八区北京时间 原始日期格式: utctime = ‘2016-07-26 10:08:29’ localtime = (datetime.datetime.fromtime ...

  7. moment.js笔记

    增加日期时间 moment().add(Number, String); moment().add(Duration); moment().add(Object); 添加天数: moment().ad ...

  8. 继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类。 (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法。 (3)子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量不能被继承。 (4)子类中定义的成员方法,并且这个方法的名字返回类型,以及参数个数和类型与父类的某个成员方法完全相同,则父类的成员方法不能被继承。 分析以上程

    继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法.(3)子类中定义的成员变量和父类中定义的 ...

  9. Buffer对象的总结

    1.首先记录下php关于字符串长度的统计 <?php echo '<br>'; echo '<br>'; 2.Buffer对象为16进制的两位数,即FF 0-225: v ...

  10. python中list和str互转

    1.list转str 假设有一个名为test_list的list,转换后的str名为test_str 则转换方法: test_str = "".join(test_list) 例子 ...