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_ ...
随机推荐
- 分析器错误(在浏览器中查看.aspx)
分析器错误 说明: 在分析向此请求提供服务所需资源时出错.请检查下列特定分析错误详细信息并适当地修改 分析器错误消息: 未能创建类型"StockManageWebService.Servic ...
- 前端MVC学习笔记(三)——AngularJS服务、路由、内置API、jQueryLite
一.服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可以被视为重复使用的执行一个或多个相关任务的代码块. AngularJS服务是单例对象, ...
- 运行ORB-SLAM笔记_使用篇(二)
1. 编译完成之后就可以使用了,按照说明我们可以知道,首先开启roscore
- 解决vs2013下创建的python文件,到其他平台(如linux)下中文乱码(或运行时报SyntaxError: (unicode error) 'utf-8' codec can't decode byte...)
Vs2013中创建python文件,在文件中没输入中文时,编码为utf-8的,如图 接着,在里面输入几行中文后,再次用notepad++查看其编码如下,在vs下运行也报错(用cmd运行就不会): 根据 ...
- table表头thead固定
<html> <head> <meta charset="utf-8"/> <script type="text/javascr ...
- Python学习笔记——基础篇【第五周】——模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- CF Round #354 Div.2
http://codeforces.com/contest/676 A. Nicholas and Permutation 题意:有一个从1到n的数列,可以任意对调两个数字一次,问数字1和n所在位置之 ...
- 搭建git代码服务器
在代码管理中,通常需要使用版本管理工具,git就是一个不错的选择,这里简单罗列一下git服务器的搭建过程. 1. 安装git工具包 2. 初始化git库:在代码服务器上,通常只需要创建一个不含有工作目 ...
- 【Python之路】第六篇--Python基础之模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- Shell学习笔记 ——第二天
1.显示日期 date | cal cal 2010 cal 2 2010 2.改变文件拥有者 chown 3.改变文件权限 chmod 4.显示当前目录 pwd 5.查看文件尾部内容,并 ...
