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 题目大意:中文题目 具体思路:莫队单点修改+区间询问模板题,在原来的区间询问的基础上,我们要记录当前这 ...
随机推荐
- 田忌赛马Java解答
你一定听过田忌赛马的故事吧? 如果3匹马变成1000匹,齐王仍然让他的马按从优到劣的顺序出赛,田忌可以按任意顺序选择他的赛马出赛.赢一局,田忌可以得到200两银子,输一局,田忌就要输掉200两 ...
- Flex与SSH集成
Flex与SSH集成 -- ::| 分类: flex |举报|字号 订阅 Flex与SSH集成 ,下载blazeds_bin_3---.zip 包,将其解压 取下blazeds.war包 更改为bla ...
- 【运维技术】slc pm 启动不了,异常排除问题记录
问题描述 slc pm 启动的时候报错如下: [root@iZuf61qpjpeuqc5mjo4kn8Z lixiang-scf-web]# slc pm strong-remoting deprec ...
- 常用php操作redis命令整理(五)ZSET类型
ZADD 向有序集合插入一个元素,元素关联一个数值,插入成功返回1,同时集合元素不可以重复, 如果元素已经存在返回 0 <?php var_dump($redis->zadd(,'A')) ...
- Python3.x:自动生成IP写入文本
Python3.x:自动生成IP写入文本 ''' 生成ip写入文件 ''' import time time_start = time.time() #参数:number-生成条数:start-开始i ...
- python3的一些练习题
下面是最近写的一些python3版本的一些练习题: 1.4+44+444+4444+.....+=? root@wadeson:~/study_for_python/homework# cat aa. ...
- POJ 1062 昂贵的聘礼(最短路)题解
题意:中文题意不解释... 思路:交换物品使得费用最小,很明显的最短路,边的权值就是优惠的价格,可以直接用Dijkstra解决.但是题目中要求最短路路径中任意两个等级不能超过m,我们不能在连最短路的时 ...
- CodeForces - 55D Beautiful numbers(数位DP+Hash)题解
题意:美丽数定义:一个正数能被所有位数整除.求给出一个范围,回答这个范围内的美丽数. 思路:一个数能被所有位数整除,换句话说就是一个数能整除所有位数的LCM,所以问题就转化为一个数能否被所有位数的LC ...
- Gym 101334F Feel Good
http://codeforces.com/gym/101334 题意:给定一串数,求一个区间,使得该区间的所有数之和乘以该区间内最小的数的乘积最大. 思路:先预处理一下,计算出前缀和. 我们可以把每 ...
- TreeMap的应用
public class SortedMap { //treemap按key排序,默认是升序,可自定义降序 public static void main(String[] args) { Map&l ...