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. 18 - csv文件-ini文件处理

    目录 1 CSV文件 1.1 手动生成一个csv文件 1.2 cvs模块 1.2.1 reader方法 1.2.2 writer方法 2 ini文件处理 2.1 configparser模块 2.2 ...

  2. 如何调整Linux内核启动中的驱动初始化顺序-驱动加载优先级

    Linux内核为不同驱动的加载顺序对应不同的优先级,定义了一些宏: include\linux\init.h #define pure_initcall(fn) __define_initcall(& ...

  3. C#.NET调用WSDL接口及方法

    1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...

  4. 打造 Laravel 优美架构 谈可维护性与弹性设计

    转载:https://juejin.im/post/5be4475c518825170559c044

  5. UVA10212 【The Last Non-zero Digit.】

    暴力可做!!!(十秒还不打暴力!!!)暴力算阶乘边算边取余上代码 #include<iostream> #define int long long //开long long using n ...

  6. SQL SERVER中查询某个表或某个索引是否存在

    查询某个表是否存在: 在实际应用中可能需要删除某个表,在删除之前最好先判断一下此表是否存在,以防止返回错误信息.在SQL SERVER中可通过以下语句实现: IF OBJECT_ID(N'表名称', ...

  7. 实际工作与JAVA面试题

    1.String 和StringBuilder.StringBuffer 的区别? 答:Java 平台提供了两种类型的字符串:String和StringBuffer / StringBuilder,它 ...

  8. 面试题:输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m

    问题: 2010年中兴面试题 编程求解: 输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来. 思路: 类似这种组合问题一般都是使 ...

  9. **PHP转义Json里的特殊字符的函数

    http://www.banghui.org/11332.html 在给一个 App 做 API,从服务器端的 MySQL 取出数据,然后生成 JSON.数据中有个字段叫 content,里面保存了文 ...

  10. winform框架源码-Devexpress开发框架

    链接: https://pan.baidu.com/s/1TnDj6qftGEUl3sTB8QXs_w 提取码: 关注公众号[GitHubCN]回复获取   开发模式:C/S C/S采用的是dev14 ...