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的更多相关文章
随机推荐
- 对AC自动机+DP题的一些汇总与一丝总结 (2)
POJ 2778 DNA Sequence (1)题意 : 给出m个病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 关键字眼:不包含,个数,长度 DP[i][j] : 表示长 ...
- webpack插件之htmlWebpackPlugin
webpack插件之htmlWebpackPlugin webpack插件 自动化 htmlWebpackPlugin 由于webpack已经帮我们处理好js之间的依赖关系,现在我们可以忽略js的加 ...
- leetcode-mid-Linked list- 200. Number of Islands¶
mycode 57.92% class Solution(object): def numIslands(self, grid): """ :type grid: Li ...
- value是列表的字典排序
# -*- coding: utf-8 -*- def dict_test(): #构造Map并对其排序 attr_tul = list(['a','b','c']) one_tul = ,],[,] ...
- Git - 对一组仓库进行配置
对一组仓库使用一套配置,另一组仓库使用另一套配置的需求也是有的,比如公司仓库的配置和我个人项目的仓库配置并不完全相同,每次都修改单个仓库的配置太麻烦并且可能会粗心忘改了以错误的配置进行提交,如何对一个 ...
- Stream的并行计算
一.Stream并行计算体验,利用多核加快计算速度 stream的并发,多个cpu执行同一个任务,提高效率: 需求:从1+...+10000000,看下各种计算方法的运行时间是多少 代码例子如下: p ...
- c++ 创建 uuid guid
如果没安装,先安装: [root@localhost]# yum install libuuid-devel #include "uuid/uuid.h" 引用 libuuid.s ...
- 正经Python汤不热爬虫
转自:https://github.com/facert/tumblr_spider install pip install -r requirements.txt run python tumblr ...
- Django聚合数据
背景: 有些时候,光靠数据库中已有字段的数据,还不足以满足一些特殊场景的需求,例如显示一个作者的所有书籍数量. 这时候就需要在已有数据基础上,聚合出这些没有的数据. 为查询集生产聚合: Django ...
- docker 一些命令
docker的基本命令 (1)创建一个虚拟机:docker-machine create --driver virtualbox default, (2)列出所有虚拟机:docker-machine ...