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的更多相关文章
随机推荐
- 《Effective Java》读书笔记 - 9.异常
Chapter 9 Exceptions Item 57: Use exceptions only for exceptional conditions 这条item的意思就是,千万不要用except ...
- 读写锁StampedLock的思想
该类是一个读写锁的改进,它的思想是读写锁中读不仅不阻塞读,同时也不应该阻塞写. 读不阻塞写的实现思路: 在读的时候如果发生了写,则应当重读而不是在读的时候直接阻塞写! 因为在读线程非常多而写线程比较少 ...
- 20175308 实验三《敏捷开发与XP实践》
20175308 实验三<敏捷开发与XP实践> 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 完成实验.撰写实验 ...
- SpringBoot整合Shiro实现权限控制,验证码
本文介绍 SpringBoot 整合 shiro,相对于 Spring Security 而言,shiro 更加简单,没有那么复杂. 目前我的需求是一个博客系统,有用户和管理员两种角色.一个用户可能有 ...
- HTTP学习记录:二、请求方法
学习资源主要为:@小坦克HTTP相关博客 最基础的是4种,GET.POST.PUT.DELETE对应着对资源的查.改.增.删.最常用的是GET和POST. GET一般用于获取/查询资源信息: POST ...
- JS获取URL中参数值
一.正则法: function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(& ...
- JavaScript —— 常见用途
javaScript 简介 第一个JavaScript 程序: 点击按钮显示日期 <!DOCTYPE html> <html> <head> <meta ...
- vue-devtools安装以后,勾选了“允许访问文件网址”之后还是无法使用
勾选了“允许访问文件网址”,还是无法使用: Vue.js is detected on this page. Devtools inspection is not available because ...
- docker--docker 网络管理
9 docker 网络管理 9.1 默认网络 1.查看docker网络: docker network ls Docker中默认的三种网络分别为bridge.host和none,其中名为bridge的 ...
- 第四周总结 and 实验二
课堂总结 一.课堂笔记总览 1.String类两种实例方法区别 String str1 = "hello";String str2 = "hello"; ...