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_ ...
随机推荐
- ASP.NET中的C#基础知识
ASP.NET中的C#基础知识 说明:asp.net作为一种开发框架现在已经广为应用,其开发的基础除了前端的html.css.JavaScript等后端最重要的语言支持还是C#,下面将主要用到的基础知 ...
- WebView使用input file控件打开相册上传图片
使用 WebView 直接用 控件选择相册图片 package com.moguzhuan.android.zhuan; import android.annotation.TargetApi; im ...
- SQL Server 2016 的「動態資料遮罩 (Dynamic Data Masking)」
一些特別注重資訊安全.個人資料的公司或產業 (如: 金融.保險業),通常「測試用資料庫」的資料,會加上「遮蔽:去識別化」的功能,避免個資外洩.以往必須自己撰寫 SQL 語句或 Stored Proce ...
- U3D简单得换装技术
四个类完成,前提是 资源得名字配合 UI按钮点击响应类 using UnityEngine; using System.Collections; public class ButtonClickHan ...
- HOOK(钩子)函数
安装.卸载钩子的相关函数 钩子类型 按功能分: 1.WH_CALLWNDPROC和WH_CALLWNDPROCRET:使你可以监视发送到窗口过程的消息3.WH_DEBUG 调试钩子4.WH_FO ...
- USB硬盘 raw之后,DiskGenius 恢复
最近,为了在E560上安装LINUX(先是gentoo,后是rhel7,最后是 centos7),用UltralISO 把一块移动硬盘给写成RAW格式了.现在又想把移动硬盘给恢复回去.采用一些方法,效 ...
- OllyDBG V1.10聆风听雨汉化版
软件名称:OllyDBG V1.10聆风听雨汉化版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 3.84MB 图片预览: 软件简介: Ollydbg2. ...
- request获取ip
public static String getIp(HttpServletRequest request) { String ip = request.getHeader("x-forwa ...
- SQL如何实现远程数据库链接
利用sp_addlinkeserver 进行远程数据库连接可以把多个数据库的数据,放置到一个数据库中, 或者有设置操作权限的情况下,我们可以通过这种方式进行查询,备份数据等操作. 首先,我们要创建连接 ...
- WIN7x64+VS2010+OpenCV2.4.10+cmake3.5.0重新编译OpenCV
1,参考博文 Win7x64+VS2012+OpenCV2.4.3+CMake2.8.10+TBB41重编译OpenCV [OpenCV入门教程之七] 玩转OpenCV源代码:生成OpenCV工程解决 ...
