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_ ...
随机推荐
- 菜鸟互啄:WINFORM如何实现无聚焦框的Button按钮
当我们将一个button按钮设置如下属性时,总有一个聚焦框来困扰着我们 button1.FlatStyle = FlatStyle.Flat; 我们想要的效果是这样的: 但当使用了Tab切换焦点时 发 ...
- Kattis - Fenwick Tree(树状数组区间更新单点求值)
Fenwick Tree Input The first line of input contains two integers NN, QQ, where 1≤N≤50000001≤N≤500000 ...
- Nuget 学习三
后期管理: 登录 nuget 官网 https://www.nuget.org/ 可以搜索到自己的包: 点击进入,可进一步操作 如果你需要给自己的类型新增其他功能,或者修改之前的bug(反正就是修改代 ...
- float 属性详解
当前元素分类(float:left) 下一个紧邻元素分类(不含float) 结论 块级元素(a) 块级元素(b) b会填充a遗留下来的空间,a会和b发生重叠,a的图层在上面. 内联元素(b) b会紧跟 ...
- JS 高效快速的数组去重
Array.prototype.uniquer = function() { var result = [], hash = {}; ; i < this.length; i++) { if ( ...
- python3 获取阿里云ECS 实例及监控的方法
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- try: import httplib except ImportError: import http.c ...
- Spring in Action --- 第一章 简介
简化java开发 基于POJO的轻量级和最小入侵性编程 通过依赖注入和面向接口实现松耦合 基于切面和管理进行声明式编程 通过切面和模板减少样板式代码 bean的生命周期 Spring对bean进行实例 ...
- DiskGenius(磁盘分区/数据恢复) 32位 V4.9.1 免费绿色版
软件名称: DiskGenius(磁盘分区/数据恢复) 32位 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 19.5MB 图片预览: 软件简介: Dis ...
- 快速掌握LODOP打印使用方法
用运之前简单介绍几个东西. install_lodop32.exe与install_lodop64.exe这两个是2个网页打印控件,网页打印必须安装这个控件在客户端,分为32和64位安装控件.(如果自 ...
- python流程控制:while循环
python编程中whihe语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务. while循环语句格式: while <判断条件>: 执行语句 count ...
