Given you a sequence of number a 1, a 2, ..., a n, 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.

InputFirst 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 a 1,a 2,...,a n.
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.OutputFor 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

题意:给定N个数,已经Q次询问,每次询问这个区间的两两GCD之和。

思路:之前做过一个类似的题,问区间两两有多少对互质,维护每个数的因子个数,即把下面的公式换成莫比乌斯系数瞎搞即可。

51nod1439:https://www.cnblogs.com/hua-dong/p/9141249.html

这里直接推。不过上次自己推的,这里我没有想出来,百度了一下。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{int l,r,id;}s[maxn];
bool cmp(in w,in v){
if(w.l/==v.l/) return w.r<v.r;
return w.l/<v.l/;
}
int a[maxn],ans[maxn],vis[maxn],phi[maxn];
int num[maxn],p[maxn],cnt,res;
vector<int>G[maxn];
void solve()
{
phi[]=;
for(int i=;i<maxn;i++){
if(!vis[i]) p[++cnt]=i,phi[i]=i-;
for(int j=;j<=cnt&&i*p[j]<maxn;j++){
phi[i*p[j]]=phi[i]*phi[p[j]]; vis[i*p[j]]=;
if(i%p[j]==){phi[i*p[j]]=phi[i]*p[j]; break;}
}
}
for(int i=;i<maxn;i++){
for(int j=i;j<maxn;j+=i) G[j].push_back(i);
}
}
void add(int x)
{
for(int i=;i<G[x].size();i++){
res+=phi[G[x][i]]*num[G[x][i]];
}
for(int i=;i<G[x].size();i++) num[G[x][i]]++;
}
void del(int x)
{
for(int i=;i<G[x].size();i++) num[G[x][i]]--;
for(int i=;i<G[x].size();i++){
res-=phi[G[x][i]]*num[G[x][i]];
}
}
int main()
{
solve();
int T,N,Q,L,R,C=;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
rep(i,,N) scanf("%d",&a[i]);
scanf("%d",&Q);
rep(i,,Q) scanf("%d%d",&s[i].l,&s[i].r),s[i].id=i;
sort(s+,s+Q+,cmp);
L=; R=; res=; memset(num,,sizeof(num));
rep(i,,Q){
while(L<s[i].l) del(a[L++]);
while(R>s[i].r) del(a[R--]);
while(L>s[i].l) add(a[--L]);
while(R<s[i].r) add(a[++R]);
ans[s[i].id]=res;
}
printf("Case #%d:\n",++C);
rep(i,,Q) printf("%d\n",ans[i]);
}
return ;
}

HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)的更多相关文章

  1. hdu 4676 Sum Of Gcd 莫队+phi反演

    Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...

  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 4676 Sum Of Gcd 莫队+数论

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

  4. 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 ...

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

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

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

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

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

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

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

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

  9. P1903 [国家集训队]数颜色 / 维护队列(莫队区间询问+单点修改)

    题目链接:https://www.luogu.org/problemnew/show/P1903 题目大意:中文题目 具体思路:莫队单点修改+区间询问模板题,在原来的区间询问的基础上,我们要记录当前这 ...

随机推荐

  1. [golang grpc] 框架介绍

    官方网站 http://www.grpc.io/ http://www.grpc.io/docs/quickstart/go.html grpc安装 • go安装 目前grpc需要go 1.5以上版本 ...

  2. python3 函数即变量的使用

    函数即变量的意思是函数被使用时后面不用(),类似变量的使用,具体如下面的示例代码: def say(name): print(name) hi = say hi('你好!') def add(): p ...

  3. Python 开发工具和框架安装

    引言: 其实之前对于 Python,只是知道有这门语言而已.大部分还是使用 .net 开发的,之前也学了 MVC+EF 开发,但是由于工作上完全用不到,也就没有在博客记录学习的东西了. 最近又接触到了 ...

  4. zoj3822

    这题说得是给了一个n*m的棋盘,每天在这个棋盘中放置一个棋子,不能放在之前已经摆放过得地方,求最后使得每行每列都有至少一个棋子的期望天数是多少,这样我们考虑怎么放,放哪里,显然数据大而且不知道状态怎么 ...

  5. 生成TPC-H数据集

    下载tpc-h tool 版本有点老,2.14.3,够用了. 在解压的文件夹下面cd到dbgen下,找到makefile.suite. ~/tpch_2_14_3$ cd dbgen~/tpch_2_ ...

  6. update t_conference set c_address = replace(c_address,'中华人民共和国','');

    update t_conference set c_address = replace(c_address,'中华人民共和国','中国');      //把c_address字段中的‘中华人民共和国 ...

  7. myeclipse安装jadclipse(反编译工具)

    我是myeclipse5. 的IDE工具.为了能反编译class文件,上网搜索了很多资料,终于找到一下的一段资料: .将jad.exe 复制到myeclipse安装目录的jre/bin目录下, 如:C ...

  8. Vue学习笔记之Vue指令系统介绍

    所谓指令系统,大家可以联想咱们的cmd命令行工具,只要我输入一条正确的指令,系统就开始干活了. 在vue中,指令系统,设置一些命令之后,来操作我们的数据属性,并展示到我们的DOM上. OK,接下来我们 ...

  9. Mysql日期转换函数、时间转换函数

    Mysql日期转换函数.时间转换函数 一.MySQL 获得当前日期时间 函数 1,获得当前日期+时间(date + time)函数:now(): select now(); 结果: :: 2,获得当前 ...

  10. FMC简介

    FMC简介 FMC ( FPGA Mezzanine Card ) 简而言之,是具有特定功能的子卡模块. Developed by a consortium of companies ranging ...