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. 利用rundll32执行程序的函数执行程序

    1.前言 无意间发现hexacorn这个国外大佬,给出了很多通过rundll32执行DLL中的函数执行程序的方法,思路很灵巧. 2.原理 rundll32加载dll 用法: rundll32 < ...

  2. git fetch 命令

    git fetch命令用于从另一个存储库下载对象和引用. 使用语法 git fetch [<options>] [<repository> [<refspec>…] ...

  3. socket相关系统调用的调用流程

    最近一直在读内核网络协议栈源码,这里以ipv4/tcp为例对socket相关系统调用的流程做一个简要整理,这些相关系统调用的内部细节虽然各有不同,但其调用流程则基本一致: 调用流程: (1)系统调用 ...

  4. TcxScheduler的使用

    TcxScheduler有两种工作模式: 一.非绑定模式 非绑定模式下,数据被存储在文件系统中.要让scheduler工作在非绑定模式下,应使TcxScheduler.Storage属性绑定到TcxS ...

  5. java使用DOM操作XML

    XML DOM简介 XML DOM 是用于获取.更改.添加或删除 XML 元素的标准. XML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档节点 每个 XML 标签是一个 ...

  6. 80端口被System占用 造成Apache不能启动的解方案

    运行netstat -aon | findstr :80 ,发现pid是4的进程占用着80端口,这还是一个系统进程,kill不掉.所以只能另想办法: 1.打开注册表:regedit 2.找到:HKEY ...

  7. Centos之命令搜索命令whereis与which

    Centos之命令搜索命令whereis与which whereis 命令名 #搜索命令所在路径及帮助文档所在位置 选项: -b :只查找可执行文件位置 -m:只查找帮助文件 [root@localh ...

  8. c 语言文本文件判断是否到达结尾的问题

    在c语言中,判断文件结尾有两种方法,第一种是使用feof()函数,feof(fp)用于测试fp所指向的文件的当前状态是否为“文件结束”.如果是,函数则返回的是非0值(真),否则为0(假),要注意的是, ...

  9. Hadoop自定义类型处理手机上网日志

    job提交源码分析 在eclipse中的写的代码如何提交作业到JobTracker中的哪?(1)在eclipse中调用的job.waitForCompletion(true)实际上执行如下方法 con ...

  10. Bootstrap进阶一:Glyphicons 字体图标

    基本组件是Bootstrap的精华之一,其中都是开发者平时需要用到的交互组件.例如:网站导航.标签页.工具条.面包屑.分页栏.提示标签.产品展示.提示信息块和进度条等.这些组件都配有jQuery插件, ...