Sum Of Gcd

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 738    Accepted Submission(s): 333

Problem Description
Given you a sequence of number a1, a2, ..., an, 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.
 
Input
First 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 a1,a2,...,an.
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.
 
Output
For 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
 思路:莫比乌兹反演+莫队;
 
然后后面的s(d)就是欧拉函数;
然后用莫队算法维护下;
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<math.h>
6 #include<queue>
7 #include<vector>
8 #include<stack>
9 #include<set>
10 using namespace std;
11 typedef long long LL;
12 int ans[100000];
13 int mul[100000];
14 typedef struct node
15 {
16 int l;
17 int r;
18 int id;
19 } ss;
20 ss ask[100000];
21 bool cmp1(node p,node q)
22 {
23 return p.l < q.l;
24 }
25 bool cmp2(node p,node q)
26 {
27 return p.r < q.r;
28 }
29 bool prime[30000];
30 int prime_table[30000];
31 vector<int>vec[30000];
32 int cnt[20005];
33 LL answ[30000];
34 int oula[20005];
35 void _slove_mo(int n,int m);
36 int main(void)
37 {
38 int n,m;
39 int T;
40 int __ca = 0;
41 int cn = 0;
42 mul[1] = 1;
43 int i,j;
44 memset(prime,0,sizeof(prime));
45 for(i = 0; i <= 20000; i++)
46 oula[i] = i;
47 for(i = 2; i <= 20000; i++)
48 {
49 if(!prime[i])
50 {
51 prime_table[cn++] = i;
52 mul[i] = -1;
53 }
54 for(j = 0; j < cn&&(i*prime_table[j]<=20000); j++)
55 {
56 if(i%prime_table[j])
57 {
58 prime[i*prime_table[j]] = true;
59 mul[i*prime_table[j]] = -mul[i];
60 }
61 else
62 {
63 prime[i*prime_table[j]] = true;
64 mul[i*prime_table[j]] = 0;
65 break;
66 }
67 }
68 }//printf("%d\n",cn);
69 for(i = 0; i < cn; i++)
70 {
71 for(j = 1; j*prime_table[i]<=20000; j++)
72 {
73 oula[j*prime_table[i]]/=prime_table[i];
74 oula[j*prime_table[i]]*=(prime_table[i]-1);
75 }
76 }
77 for(i = 1; i <= 20000; i++)
78 {
79 for(j = 1; j <= sqrt(i); j++)
80 {
81 if(i%j==0)
82 {
83 vec[i].push_back(j);
84 if(i/j != j)
85 vec[i].push_back(i/j);
86 }
87 }
88 }scanf("%d",&T);
89 while(T--)
90 {
91 ++__ca; memset(cnt,0,sizeof(cnt));
92 scanf("%d",&n);
93 for(i = 1; i <= n; i++)
94 {
95 scanf("%d",&ans[i]);
96 }
97 scanf("%d",&m);
98 for(i = 0; i < m; i++)
99 {
100 scanf("%d %d",&ask[i].l,&ask[i].r);
101 ask[i].id = i;
102 }
103 sort(ask,ask+m,cmp1);
104 int id = 0;
105 int ak = sqrt(1.0*n)+1;
106 int v = ak;
107 for(i = 0; i < m; i++)
108 {
109 if(ask[i].l > v)
110 {
111 v += ak;
112 sort(ask+id,ask+i,cmp2);
113 id = i;
114 }
115 }
116 sort(ask+id,ask+m,cmp2);
117 _slove_mo(n,m);
118 printf("Case #%d:\n",__ca);
119 for(i = 0; i < m; i++)
120 printf("%lld\n",answ[i]);
121
122 }return 0;
123 }
124 void _slove_mo(int n,int m)
125 {
126 int i,j;
127 LL sum = 0;
128 int xl = ask[0].l;
129 int xr = ask[0].r;
130 for(i = xl; i <= xr; i++)
131 {
132 for(j = 0; j < vec[ans[i]].size(); j++)
133 { int x = vec[ans[i]][j];
134 sum = sum + (LL)oula[x]*(LL)cnt[x];
135 cnt[x]++;
136 }
137 }
138 answ[ask[0].id] = sum;
139 for(i = 1; i < m; i++)
140 {
141 while(xl < ask[i].l)
142 {
143 int y = ans[xl];
144 for(j = 0; j < vec[y].size(); j++)
145 {
146 int x = vec[y][j];
147 sum -= (LL)oula[x]*(LL)(--cnt[x]);
148 }
149 xl++;
150 }
151 while(xl > ask[i].l)
152 {
153 xl--;
154 int y = ans[xl];
155 for(j = 0; j < vec[y].size(); j++)
156 {
157 int x = vec[y][j];
158 sum += (LL)oula[x]*(LL)(cnt[x]++);
159 }
160 }
161 while(xr > ask[i].r)
162 {
163 int y = ans[xr];
164 for(j = 0; j < vec[y].size(); j++)
165 {
166 int x = vec[y][j];
167 sum -= (LL)oula[x]*(LL)(--cnt[x]);
168 }
169 xr--;
170 }
171 while(xr < ask[i].r)
172 {
173 xr++;
174 int y = ans[xr];
175 for(j = 0; j < vec[y].size(); j++)
176 {
177 int x = vec[y][j];
178 sum += (LL)oula[x]*(LL)(cnt[x]++);
179 }
180 }
181 answ[ask[i].id] = sum;
182 }
183 }

Sum Of Gcd(hdu 4676)的更多相关文章

  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 (莫队&区间gcd公式)

    Given you a sequence of number a 1, a 2, ..., a n, which is a permutation of 1...n. You need to answ ...

  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 5381 The sum of gcd(线段树+gcd)

    题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...

  6. 【HDU 5381】 The sum of gcd (子区间的xx和,离线)

    [题目] The sum of gcd Problem Description You have an array A,the length of A is nLet f(l,r)=∑ri=l∑rj= ...

  7. hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  8. 2015 Multi-University Training Contest 8 hdu 5381 The sum of gcd

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

随机推荐

  1. 网易云信 集成UI库登录dologin没有回调

    感谢github上的两位大佬指出问题的解决方法. 解决方法: 在进行ui初始化要在主进程中进行,初始化前进行主进程判断. 若还收不到回调,可尝试将uikit中的base包去掉而在build.gradl ...

  2. C/C++ Qt 数据库与TableView多组件联动

    Qt 数据库组件与TableView组件实现联动,以下案例中实现了,当用户点击并选中TableView组件内的某一行时,我们通过该行中的name字段查询并将查询结果关联到ListView组件内,同时将 ...

  3. 25. Linux下gdb调试

    1.什么是core文件?有问题的程序运行后,产生"段错误 (核心已转储)"时生成的具有堆栈信息和调试信息的文件. 编译时需要加 -g 选项使程序生成调试信息: gcc -g cor ...

  4. Leetcode中的SQL题目练习(一)

    595. Big Countries https://leetcode.com/problems/big-countries/description/ Description name contine ...

  5. 初学js正则表达式之密码强度验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. RocketMQ集群搭建方式

    各角色介绍 Producer:消息的发送者:举例:发信者 Consumer:消息接收者:举例:收信者 Broker:暂存和传输消息:举例:邮局 NameServer:管理Broker:举例:各个邮局的 ...

  7. react动态添加样式:style和className

    react开发过程中,经常会需要动态向元素内添加样式style或className,那么应该如何动态添加呢??? 一.react向元素内,动态添加style 例如:有一个DIV元素, 需要动态添加一个 ...

  8. jdk1.6,1.7,1.8解压版无需安装(64位)

    1.java SE 1.6各个版本 jdk http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads ...

  9. js处理title超长问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. linux重启后JDk环境变量配置失效最终解决方案

    最终解决方案:https://bbs.deepin.org/forum.php?mod=viewthread&tid=147762 其实这个修改可能也存在问题,如果有耐心的可以每次打开终端   ...