HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)
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公式)的更多相关文章
- hdu 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
- HDU 4676 Sum Of Gcd 【莫队 + 欧拉】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...
- hdu 4676 Sum Of Gcd 莫队+数论
题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...
- 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 ...
- HDU-4676 Sum Of Gcd 莫队+欧拉函数
题意:给定一个11~nn的全排列AA,若干个询问,每次询问给出一个区间[l,r][l,r],要求得出∑l≤i<j≤r gcd(Ai,Aj)的值. 解法:这题似乎做的人不是很多,蒟蒻当然不会做只 ...
- hdu5381 The sum of gcd]莫队算法
题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...
- Hdu5381-The sum of gcd(莫队)
题意我就不说了 解析: 莫队,先预处理出以i为右端点的区间的gcd值,有一些连续的区间的gcd值是相同的,比如[j,i],[j+1,i],[j+2,i]的gcd值是相同的,我们可以把[j,j+2] ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- P1903 [国家集训队]数颜色 / 维护队列(莫队区间询问+单点修改)
题目链接:https://www.luogu.org/problemnew/show/P1903 题目大意:中文题目 具体思路:莫队单点修改+区间询问模板题,在原来的区间询问的基础上,我们要记录当前这 ...
随机推荐
- Django的FBV和CB
Django的FBV和CBV FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV C ...
- 泰德激光打标软件 包含 #include "Main.h" 时 原本正确的单元却报错
问题:泰德激光打标软件 ,当新增单元需要包含 #include "Main.h" 时, 原本正确的单元却报错. 办法:包含 #include "Main.h" ...
- Python 字符串转换为日期
应用程序接受字符串格式的输入,但是你想将它们转换为datetime 对象以便在上面执行非字符串操作. 使用Python 的标准模块datetime 可以很容易的解决这个问题.比如: >>& ...
- Java 对比Vector、ArrayList、LinkedList
①引言 在日常生活中能高效的管理和操作数据是非常重要的.Java提供了强大的集合框架,大大提高了开发者的生产力,今天就了解一下有关集合框架方面的问题. Vector.ArrayList.LinkedL ...
- js动态创建Form表单并提交
javascript动态创建Form表单和表单项,然后提交表单请求,最后删除表单,代码片段如下(Firefox测试通过): var dlform = document.createElement('f ...
- Jsuop Whitelist
Jsuop使用示例代码 使用jsoup HTML Cleaner 方法进行清除,但需要指定一个可配置的 Whitelist.http://jsoup.org/apidocs/org/jsoup/saf ...
- iOS App迁移(App Transfer)注意点
1.App迁移需要苹果审核吗? 答:不需要 2.App迁移需要多长时间? 答:迁移操作过程很快,A账号发出申请,B账号接收,几分钟时间.App Store 展示B账号相关信息可能几分钟,也可能有延迟几 ...
- @Bean 生命周期
bean生命周期: 实例bean 1.当调用者通过getBean(beanName)向容器请求某一个Bean时,如果容器注册了org.springframework.beans.factory.con ...
- TCGA系列--TCGA可视化数据库GEPIA
中国大牛力作 张泽民: http://gepia.cancer-pku.cn/index.html http://cancer-pku.cn/
- Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学
C. Vasya and Petya's Game time limit per test 1 second memory limit per test 256 megabytes input sta ...