UVa10426
GCD Extreme(II)
Input: Standard Input Output:
Standard Output
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
Output for Sample Input
67 13015 143295493160
Problemsetter: Shahriar Manzoor
Special Thanks: Syed Monowar Hossain
/* 【题意】
求sum(gcd(i,j),1<=i<j<=n)1<n<4000001
【题解】 1.建立递推关系,s(n)=s(n-1)+gcd(1,n)+gcd(2,n)+……+gcd(n-1,n);
2.设f(n)=gcd(1,n)+gcd(2,n)+……+gcd(n-1,n)。
gcd(x,n)=i是n的约数(x<n),按照这个约数进行分类。设满足gcd(x,n)=i的约束有g(n,i)个,则有f(n)=sum(i*g(n,i))。
而gcd(x,n)=i等价于gcd(x/i,n/i)=1,因此g(n,i)等价于phi(n/i).phi(x)为欧拉函数。
3.降低时间复杂度。用筛法预处理phi[x]表
用筛法预处理f(x)->枚举因数,更新其所有倍数求解。
*/
/* 欧拉函数的应用,以后看到互质的数第一个就要想到欧拉函数。今天又学到了好多家伙。 欧拉定理: 欧拉定理表明,若n,a为正整数,且n,a互质,(a,n) = 1,则a^φ(n) ≡ 1 (mod n) 费马小定理: 且(a,p)=1,那么 a^(p-1) ≡1(mod p) 假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1 */
//1285ms #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; const int M=;
int phi[M];
ll s[M],f[M];//f[n]=sum(phi[i]*j) (i*j=n) (去掉n*phi[1],因为gcd(n,n)不符合题意!!) int get(){
char c;
int res=;
while(c=getchar(),!isdigit(c));
do{
res=(res<<)+(res<<)+(c-'');
}while(c=getchar(),isdigit(c));
return res;
} void phi_table()//类似素数刷表!!
{
phi[]=;
for(int i=;i<M;i++)phi[i]=i;
for(int i=;i<M;i+=)phi[i]/=;
for(int i=;i<M;i+=)
if(phi[i]==i)//说明i是素数!!!
for(int j=i;j<M;j+=i)
{
phi[j]-=phi[j]/i;//保证i是素数且是j的素因子!!!
}
} int main()
{
int n,i,j,k;
phi_table();
memset(f,,sizeof(f));
phi[]=;
for(i=;i<(int)sqrt(M);i++)
{
f[i*i]+=phi[i]*i;
for(n=i*i+i;n<M;n+=i)
f[n]+=(ll)(i*phi[n/i]+n/i*phi[i]);//之前令phi[1]=0;因为gcd(n,n)不符合题意!!
}
s[]=;
for( n=;n<M;n++) s[n]=s[n-]+f[n];
while()
{
n=get();
if(n==)break;
printf("%lld\n",s[n]);
}
return ;
} //2382ms
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; const ll M=;
ll phi[M];
ll s[M],f[M]; void phi_table()//类似素数刷表!!
{
phi[]=;
for(int i=;i<M;i++)phi[i]=i;
for(int i=;i<M;i+=)phi[i]/=;
for(int i=;i<M;i+=) if(phi[i]==i)//说明i是素数!!!
for(int j=i;j<M;j+=i)
{
phi[j]-=phi[j]/i;//保证i是素数且是j的素因子!!!
}
}
int main()
{
phi_table();
memset(f,,sizeof(f));
for(int i=;i<M;i++)
for(int n=i+i;n<M;n+=i)
f[n]+=i*phi[n/i];
s[]=;
for(int n=;n<M;n++) s[n]=s[n-]+f[n];
int n;
while(scanf("%d",&n)==&&n)
{
printf("%lld\n",s[n]);
}
return ;
} #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; const ll M=;
ll phi[*M];
ll s[M],f[M]; void phi_table()//类似素数刷表!!
{
for(int i=;i<=M;i++)phi[i]=;
phi[]=;
for(int i=;i<=M;i++)
if(!phi[i])//说明i是素数!!!
for(int j=i;j<=M;j+=i)
{
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-);//保证i是素数且是j的素因子!!!
}
}
int main()
{
phi_table();
memset(f,,sizeof(f));
for(int i=;i<=M;i++)
for(int n=i+i;n<=M;n+=i)
f[n]+=i*phi[n/i];
s[]=;
for(int n=;n<=M;n++) s[n]=s[n-]+f[n];
int n;
while(scanf("%d",&n)==&&n)
{
printf("%lld\n",s[n]);
}
return ;
}
UVa10426的更多相关文章
随机推荐
- 四、IDEA创建SpringBoot项目
1.从官网下载之后直接导入IDEA: 下载完成解压之后如下图: IDEA导入该项目: 之后一路next即可 导入成功之后你可能会发现左下角一直有个进度条在进行,傻傻的同学可能以为是在下载jar包,下个 ...
- C# 开发COM组件供c++使用
C# 开发COM组件供c++使用 Microsoft在解决和以往的COM和SDK开发技术之间的互操作性(Interoperability)方面做了很多的工作,其中包括COM和.NET对象之间的相互调用 ...
- hibernate+spring mvc,解决hibernate对象懒加载,json序列化失败
在使用spring MVC时,@ResponseBody 注解的方法返回一个有懒加载对象的时候出现了异常,以登录为例: @RequestMapping("login") @Resp ...
- SAP屏幕事件的控制
1. INITALIZATION事件 该事件在屏幕未显示之前执行,对程序设置值及屏幕元素进行初始化赋值. REPORT Y001. PARAMETERS QUAL_DAY TYPE D DEFAUL ...
- 虚拟机中Ubuntu安装及基本功能设置
虚拟机下安装ubuntu 虚拟机使用VMware14 PRO,在TOSHIBA EXT/Anon Comm Group\Experimental Environment\VMware下. 系统使用ub ...
- 2d平台怪物逻辑
2d来回巡逻 遇到坑会自动转向 可配置单次方向行走的时间,转向等待时间等 using System; using System.Collections; using System.Collection ...
- (一)VS2015下配置OpenGL
刚开始用OpenGL,一开始不太明白配置库的原理,在VS2015下耗费了大量时间.这里将配置过程做个笔记,以供日后查看.配置过程中,需要下载cmake构建工具以及glew和glfw库. 下载地址为: ...
- PHPFPM模式三种运行模式
1.static模式 static模式始终会保持一个固定数量的子进程,这个数量由pm.max_children定义. 2.dynamic模式 子进程的数量是动态变化的,启动时,会生成固定数量的子进 ...
- python 并发编程 多进程 守护进程
一 守护进程 主进程创建子进程目的是:主进程有一个任务需要并发执行,那开启子进程帮我并发执行任务 主进程创建子进程,然后将该进程设置成守护自己的进程 关于守护进程需要强调两点: 其一:守护进程会在主进 ...
- 【Linux U-boot】U-Boot相关命令
<1> help --帮助命令环境变量相关: <2> printenv --查看环境变量也可以使用缩写:print 查看某一个环境变量的值,例子:printenv bootar ...