题目链接

给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j。gcd(a[i], a[j]) L <= i < j <= R的和。

假设两个数的公约数有b1, b2, b2...bn, 那么这两个数的最大公约数就是phi[b1] + phi[b2] + phi[b3]...+phi[bn]。

知道这个就可以用莫队了, 具体看代码。

#include <bits/stdc++.h>

using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = ;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 2e4+;
vector <int> v[maxn];
struct node
{
int l, r, block, id;
}q[maxn];
bool cmp(const node& a, const node& b)
{
if(a.block == b.block)
return a.r < b.r;
return a.block < b.block;
}
int phi[maxn], cnt[maxn], a[maxn];
ll res, ans[maxn];
void add(int x)
{
for(int i = ; i < v[x].size(); i++) {
res += phi[v[x][i]]*(cnt[v[x][i]]++);
}
}
void sub(int x)
{
for(int i = ; i < v[x].size(); i++) {
res -= phi[v[x][i]]*(--cnt[v[x][i]]);
}
}
void solve(int m)
{
int L = , R = ;
res = ;
mem(cnt);
for(int i = ; i < m; i++) {
while(R < q[i].r) {
add(a[++R]);
}
while(R > q[i].r) {
sub(a[R--]);
}
while(L < q[i].l) {
sub(a[L++]);
}
while(L > q[i].l) {
add(a[--L]);
}
ans[q[i].id] = res;
}
}
void init()
{
for(int i = ; i < maxn; i++) {
phi[i] = i;
for(int j = i; j < maxn; j += i) {
v[j].pb(i);
}
}
for(int i = ; i < maxn; i++) {
if(phi[i] == i) {
for(int j = i; j < maxn; j += i) {
phi[j] = phi[j]/i*(i-);
}
}
}
}
int main()
{
init();
int t, n, m;
cin>>t;
for(int casee = ; casee <= t; casee++) {
scanf("%d", &n);
int BLOCK = sqrt(n*1.0);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
for(int i = ; i < m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].block = q[i].l/BLOCK;
q[i].id = i;
}
sort(q, q+m, cmp);
solve(m);
printf("Case #%d:\n", casee);
for(int i = ; i < m; i++) {
printf("%lld\n", ans[i]);
}
}
}

hdu 4676 Sum Of 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 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. Android学习自定义Dialog

    Dialog是Android提供的各种对话框的基类,和上篇的DialogFragment类似.为什么还要介绍Dialog呢,因为DialogFragment只能运行在Android3.0以上的系统中. ...

  2. radio与checkbox

    最近一直在学习Javascript与asp.net MVC4,每天都在跟着书学习.这样总感觉自己看的很抽象,没有点实际的意义.而且,每次看的东西很容易忘记,所以打算在这里记录自己的学习笔记. Java ...

  3. Scala基础入门-4

    Scala学习——类 简单类和无参方法 class Counter { private var value = 0 // 必须初始化字段 def increment() { value += 1 } ...

  4. mysql错误:Error Code: 1175. You are using safe update mode and you tried to update a table……

    今天遇到一个mysql错误:   Error Code: . You are using safe update mode and you tried to update a table withou ...

  5. Python下的机器学习工具sklearn--数据预处理

    1.数据标准化(Standardization or Mean Removal and Variance Scaling) 进行标准化缩放的数据均值为0,具有单位方差. from sklearn im ...

  6. IO-03. 求整数均值

    /** *A3-IO-03. 求整数均值(10) *C语言实现 *测试已通过 */ #include "stdio.h" int main() { int a,s,d,f; sca ...

  7. linux查看网卡吞吐量和网卡流量用自带命令,iptraf查看。

    1 命令行直接输入:iptraf(如果没有,使用yum install iptraf安装) 2.打开进入选择ip镜像. 3. 选择监听测试的接口,所有接口. 4.开启监听状态日志,系统默认日志路径./ ...

  8. 正确决解Hibernate4.*中:Connection cannot be null when 'hibernate.dialect' not set

    <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  9. CC EAL认证

    国际通用准则(CC) CC(Common Criteria)是国际标准化组织统一现有多种准则的结果,是目前最全面的评价准则.1996年6月,CC第一版发布:1998年5月,CC第二版发布:1999年 ...

  10. Android AndroidManifest 清单文件以及权限详解!【转】

    转自:http://my.oschina.net/yuanxulong/blog/366753 每个Android应用都需要一个名为AndroidManifest.xml的程序清单文件,这个清单文件名 ...