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. mysql查询表和字段的注释

    1,新建表以及添加表和字段的注释.   create table t_user(        ID INT(19) primary key auto_increment  comment '主键', ...

  2. 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]

    一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...

  3. html结构和标签

    <!DOCTYPE html><meta charset="utf-8"><header>表示页面的一个内容区块,或整个页面的标题</he ...

  4. MySQL从删库到跑路(三)——SQL语言

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.SQL语言简介 1.SQL语言简介 SQL是结构化查询语言(Structured Query Language) ...

  5. python 手动遍历迭代器

    想遍历一个可迭代对象中的所有元素,但是却不想使用for 循环 为了手动的遍历可迭代对象,使用next() 函数并在代码中捕获StopIteration 异常.比如,下面的例子手动读取一个文件中的所有行 ...

  6. Python: 字符串格式化format()函数的使用

    python从2.6开始支持format,新的更加容易读懂的字符串格式化方法,从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序号, ...

  7. CentOS6.5下Cloudera安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)

     不多说,直接上干货! 第一步: Cloudera Manager安装之Cloudera Manager安装前准备(CentOS6.5)(一) 第二步: Cloudera Manager安装之时间服务 ...

  8. 2017-2018-1 JaWorld 第八周作业

    2017-2018-1 JaWorld 第八周作业 团队分工 成员 分工 陈是奇 统计成员工具选择 马平川 类图 王译潇 编码规范 李昱兴 用例图 林臻 状态图 张师瑜 推进工作进展.写博客 UML ...

  9. ES7学习笔记——Array.prototype.includes和求幂运算符**

    一直以来,在前端开发时使用的基本都是ES5,以及少量的ES6.3月份换工作面试时,发现一些比较大的公司,对ES6比较重视,阿里的面试官直接问ES7和ES8,对于从未接触过人来说,完全是灾难.由此也显现 ...

  10. Recover Binary Search Tree,恢复二叉排序树

    问题描述:题意就是二叉树中有两个节点交换了,恢复结构. Two elements of a binary search tree (BST) are swapped by mistake. Recov ...