1188 最大公约数之和 V2
第1行:1个数T,表示后面用作输入测试的数的数量。(1 <= T <= 50000)
第2 - T + 1行:每行一个数N。(2 <= N <= 5000000)
共T行,输出最大公约数之和。
3
10
100
200000
67
13015
143295493160
思路:欧拉函数;
http://www.cnblogs.com/zzuli2sjy/p/5831575.html是这个题的加强版,这里用筛法求欧拉函数,然后再用类似筛法的方法求每个数的约数对答案的贡献,最后求下前缀和
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<math.h>
7 #include<set>
8 #include<vector>
9 #include<string.h>
10 using namespace std;
11 typedef long long LL;
12 bool prime[5000005];
13 int oula[5000005];
14 int ans[5000005];
15 LL ask[5000005];
16 int main(void)
17 {
18 int i,j;
19 int T,N;
20 for(i = 2; i <= 3000; i++)
21 {
22 if(!prime[i])
23 {
24 for(j = i; (i*j)<=5000005; j++)
25 {
26 prime[i*j]=true;
27 }
28 }
29 }
30 int cn = 0;
31 for(i = 2; i <= 5000000; i++)
32 {
33 if(!prime[i])
34 {
35 ans[cn++]=i;
36 }
37 }
38 for(i = 0; i <= 5000000 ; i++)
39 oula[i]=i;
40 oula[1]=0;
41 memset(ask,0,sizeof(ask));
42 for(i = 0; i < cn; i++)
43 {
44 for(j = 1; j*ans[i] <= 5000000 ; j++)
45 {
46 oula[j*ans[i]]/=ans[i];
47 oula[j*ans[i]]*=ans[i]-1;
48 }
49 }
50 for(i = 1; i <= 5000000; i++)
51 {
52 for(j = 2; (i*j) <= 5000000; j++)
53 {
54 ask[i*j]+=oula[j]*i;
55 }
56 }
57 for(i = 2;i <= 5000000; i++)
58 {
59 ask[i]+=ask[i-1];
60 }
61 scanf("%d",&T);
62 while(T--)
63 {
64 scanf("%d",&N);
65 printf("%lld\n",ask[N]);
66 }
67 return 0;
68 }
1188 最大公约数之和 V2的更多相关文章
- 51 nod 1188 最大公约数之和 V2
1188 最大公约数之和 V2 题目来源: UVA 基准时间限制:2 秒 空间限制:262144 KB 分值: 160 难度:6级算法题 给出一个数N,输出小于等于N的所有数,两两之间的最大公约数 ...
- 51nod - 1188 - 最大公约数之和 V2 - 数论
https://www.51nod.com/Challenge/Problem.html#!#problemId=1188 求\(\sum\limits_{i=1}^{n-1}\sum\limits_ ...
- 51nod 1188 最大公约数之和 V2
第二个\( O(T\sqrt(n)) \)复杂度T了..T了..T了...天地良心,这能差多少?! 于是跑去现算(. \[ \sum_{i=1}^{n-1}\sum_{j=i+1}^{n}gcd(i, ...
- 51nod1188 最大公约数之和 V2
考虑每一个数对于答案的贡献.复杂度是O(nlogn)的.因为1/1+1/2+1/3+1/4......是logn级别的 //gcd(i,j)=2=>gcd(i/2,j/2)=1=>phi( ...
- [51nod1188]最大公约数之和 V2(筛法)
题面 传送门 题解 口胡的整除分块单次询问\(O(\sqrt{n})\)的做法居然\(T\)了?那还是好好看正解吧-- 首先我们枚举\(j\),求对于每个\(j\)有所有\(i<j\)的\(\g ...
- 51Nod 最大公约数之和V1,V2,V3;最小公倍数之和V1,V2,V3
1040 最大公约数之和 给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6 1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 输入 1个数N ...
- 51nod 1237 最大公约数之和 V3(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...
- 51NOD 1237 最大公约数之和 V3 [杜教筛]
1237 最大公约数之和 V3 题意:求\(\sum_{i=1}^n\sum_{j=1}^n(i,j)\) 令\(A(n)=\sum_{i=1}^n(n,i) = \sum_{d\mid n}d \c ...
- 51nod 1040 最大公约数之和(欧拉函数)
1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出一个n,求1-n这n个数,同n的最大公约数的和.比如: ...
随机推荐
- Linux-普通用户和root用户任意切换
普通用户切换为root: 1.[xnlay@bogon ~]$含义:xnlay代表当前用户,bogon指的是主机名,~表示当前用户,$表示普通用户:[root@bogon ~]#root代表是超级用户 ...
- 29-Regular Expression Matching-leetcode
'.' Matches any single character. '*' Matches zero or more of the preceding element. The matching sh ...
- android studio 编译 Android dependency has different version
找了一圈,终于在大佬的博客中找到了解决方法. 附链接:https://blog.csdn.net/u010725171/article/details/81232183 Android depende ...
- Scala【json字符串和json对象互相转换】
一.fastjson工具 pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...
- webpack打包报错 ERROR in ./js/ww.js from UglifyJs Unexpected token keyword «function», expected punc «,» [src/page/ww/view/xx/xx.vue:119,0][./js/ww.js:55218,17]
找了好多解决办法 你可以试着将babel-loader的exclude注释掉,然后看能否打包成功.如果可以,那就是这个问题.你只需要在vue.config.js中配置transpileDependen ...
- iOS 的文件操作
直接上操作 效果:将一张图片写入文件 (图片本身已经在Assets.xcassets里面了) 1.获取当前app的沙盒路径 NSString *documentPath = NSSearchPathF ...
- fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to connect to github.com port 443: Timed out
今天使用git push的时候提示"fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to conne ...
- Ajax异步更新网页(使用原生JavaScript)
一.页面代码 <!DOCTYPE html> <html> <head> <title>MyHtml.html</title> <me ...
- 【C#】【MySQL】C#连接MySQL数据库(一)代码
C#连接MySQL数据库 准备工作 1.环境安装 安装MySQL For Visual Studio<<点击进入官网下载 第一个要下载安装,第二个下载后将MySQL.data添加到Visu ...
- SpringBoot自定义控制层参数解析
一.背景 在Spring的Controller中,我们通过@RequestParam或@RequestBody就可以将请求中的参数映射到控制层具体的参数中,那么这个是怎么实现的呢?如果我现在控制层中的 ...