UVa 11426
首先我们了解一下欧拉函数phi[x],phi[x]表示的是不超过x且和x互素的整数个数
phi[x]=n*(1-1/p1)*(1-1/p2)....(1-1/pn);
计算欧拉函数的代码为
int euler_phi(int n){
int m=(int)sqrt(n+0.5);
int ans=n;
for(int i=;i*i<=m;i++){
if(n%i==){//
ans=ans/i*(i-);
while(n%i==)n=n/;
}
}
if(n>)ans=ans/n*(n-);
return ans;
}
也可以通过类似于素数打表的方法来对欧拉函数进行打表
void phi_table(int n){
for(int i=;i<=n;i++)phi[i]=;
phi[]=;
for(int i=;i<=n;i++){
if(phi[i]==)
for(int j=i;j<=n;j=j+i){
if(phi[j]==)phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
}
题意
Given the value of N, you will have to find the value of G. The definition of G is given below:
|
|
Here GCD(i,j) means the greatest common divisor of integer i and integer j.
For those who have trouble understanding summation notation, the meaning of G is given in the following code:
|
G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } /*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/ |
Input
The input file contains at most 100 lines of inputs. Each line contains an integer N (1<N<4000001). The meaning of N is given in the problem statement. Input is terminated by a line containing a single zero.
Output
For each line of input produce one line of output. This line contains the value of G for the corresponding N. The value of G will fit in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160
设f(n)=gcd(1,2)+gcd(1,3)+gcd(2,3)+....+gcd(n-1,n); 题意是求1<=i<j<=n的数对(i,j)所对应的gcd(i,j)的和s(j)
s(n)=f(1)+f(2)+f(3)+f(4)+....f(n);
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
ll S[N],f[N];
ll phi[N];
ll euler_phi(int n){
int m=(int)sqrt(n+0.5);
ll ans=n;
for(int i=;i*i<=m;i++){
if(n%i==){//2一定是素因子
ans=ans/i*(i-);
while(n%i==)n=n/;
}
}
if(n>)ans=ans/n*(n-);
return ans;
}
void phi_table(int n){
for(int i=;i<=n;i++)phi[i]=;
phi[]=;
for(int i=;i<=n;i++){
if(phi[i]==)
for(int j=i;j<=n;j=j+i){
if(phi[j]==)phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
}
void init(){
int n=N;
phi_table(N);
memset(f,,sizeof(f));
for(int i=;i<=n;i++){
for(int j=i*;j<=n;j=j+i)f[j]=f[j]+i*phi[j/i];
}
memset(S,,sizeof(S));
S[]=f[];
for(int i=;i<=n;i++)S[i]=S[i-]+f[i];
}
int main(){
int n;
init();
while(scanf("%d",&n)!=EOF){
//init(n);
if(n==)break;
printf("%lld\n",S[n]);
}
}
UVa 11426的更多相关文章
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)
题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...
- UVA 11426 GCD-Extreme(II) ★ (欧拉函数)
题意 求Σ{1<=i<N} Σ{i<j<=N} GCD(i, j) (N<=4000000) 分析 原始思路 暴力求明显是不行的,我们把式子简化形式一下发现它可以 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- UVA 11426 GCD - Extreme (II) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
- UVA 11426 GCD Extrme (Ⅲ)
给定一个整数N(1<N<=4000000)的整数求∑GCD(i,j)i=1,2,3....j-1,2<=j<=n的值.参考了一下网上的题解,复述一下我理解后的思路,加深理解: ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- 【UVA 11426】gcd之和 (改编)
题面 \(\sum_{i=1}^{n}\sum_{j=1}^m\gcd(i,j)\mod998244353\) \(n,m<=10^7\) Sol 简单的一道莫比乌斯反演题 \(原式=\sum_ ...
随机推荐
- 用浅/深拷贝、和HTML5方法解决js对象的引用的问题
先来看一个例子 例一: var a=[1,2,3]; var b=a; b.push(4); alert(b);//1,2,3,4 alert(a);//1,2,3,4 var a=[1,2,3]; ...
- template template parameter
#include <iostream> using namespace std; template<typename T> class A { }; template<t ...
- CSS控制文本在一行内显示,若有多余字符则使用省略号表示
强制文本在一行内显示,多余字符使用省略号 text-overflow: ellipsis; overflow: hidden; white-space: nowrap;
- Dockerfile文件格式的简单介绍
# This dockerfile uses the ubuntu image # VERSION 2 - EDITION 1 # Author: docker_user # Command form ...
- websocket+前后端分离+https的nginx配置
后端服务路径: 172.168.0.2:8080 172.168.0.2:7080 前端目录(html + css + js): /root/apps/mzsg-web 1.修改 /etc/nginx ...
- mysql java写入时间少14小时
查看时区: mysql> show variables like '%time_zone%'; +------------------+--------+ | Variable_name | V ...
- Maven之(九)依赖关系
在maven的管理体系中,各个项目组成了一个复杂的关系网,但是每个项目都是平等的,是个没有贵贱高低,众生平等的世界,全球每个项目从理论上来说都可以相互依赖.就是说,你跟开发spring的大牛们平起平坐 ...
- C# 语言规范_版本5.0 (第19章 附录A_文档注释)
A. 文档注释 C# 提供一种机制,使程序员可以使用含有 XML 文本的特殊注释语法为他们的代码编写文档.在源代码文件中,可以使用特定形式的注释来指导工具从这些注释及其后的源代码元素生成 XML.使用 ...
- php核心编程
搭建web服务器的环境(配置PHP的工作环境): 首先要配置php,在Apache的配置文件夹中httpd.conf中配置 1把php配置成Apache的一个功能模块 LoadModule php5_ ...
- ContourLine
#define MULTI_PLOT true //Determine whether or not to plot multiple iterations. #define X_MAX 1.0 // ...
