uva 11426 线性欧拉函数筛选+递推
Problem J 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
/*
题目大意给出一个n,求sum(gcd(i,j),0<i<j<=n);
可以明显的看出来s[n]=s[n-1]+f[n];
f[n]=sum(gcd(i,n),0<i<n);
现在麻烦的是求f[n]
gcd(x,n)的值都是n的约数,则f[n]=
sum{i*g(n,i),i是n的约数},注意到gcd(x,n)=i的
充要条件是gcd(x/i,n/i)=1,因此满足条件的
x/i有phi(n/i)个,说明gcd(n,i)=phi(n/i).
f[n]=sum{i*phi(n/i),1=<i<n}
因此可以搞个for循环对i循环,只要i<n,f[n]+=i*phi(n/i);
*/
#include<iostream>
#include<cstdio>
using namespace std;
#define Max 4000000 __int64 s[Max+],f[Max+],phi[Max+];
int prime[Max/];
bool flag[Max+]; void Init()
{
int i,j,num=;
memset(flag,,sizeof(flag));
phi[]=;
for(i=;i<=Max;i++)//欧拉筛选
{
if(flag[i])
{
prime[num++]=i;
phi[i]=i-;
}
for(j=;j<num && prime[j]*i<=Max;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
memset(f,,sizeof(f));
for(i=;i<=Max;i++)//f[n]更新
{
//因为f[n]=gcd[1,n]+....+gcd[n-1,n]所以j=2*i开始,若从j=i开始那就等同于加上了一项gcd(n,n)
for(j=*i;j<=Max;j+=i)
f[j]+=i*phi[j/i];
}
memset(s,,sizeof(s));
for(i=;i<=Max;i++)
s[i]=s[i-]+f[i];
} int main()
{
Init();
int n;
while(cin>>n,n)
printf("%I64d\n",s[n]);
return ;
}
uva 11426 线性欧拉函数筛选+递推的更多相关文章
- GCD - Extreme (II) UVA - 11426(欧拉函数!!)
G(i) = (gcd(1, i) + gcd(2, i) + gcd(3, i) + .....+ gcd(i-1, i)) ret = G(1) + G(2) + G(3) +.....+ G(n ...
- POJ_2478 Farey Sequence 【欧拉函数+简单递推】
一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...
- UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)
pid=26358">https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&cate ...
- 紫书 例题 10-7 UVa 10820 (欧拉函数)
这道题要找二元组(x, y) 满足1 <= x, y <= n 且x与y互素 那么我就可以假设x < y, 设这时答案为f(n) 那么答案就为2 * f(n) +1(x与y反过来就乘 ...
- 紫书 例题 10-27 UVa 10214(欧拉函数)
只看一个象限简化问题,最后答案乘4+4 象限里面枚举x, 在当前这条固定的平行于y轴的直线中 分成长度为x的一段段.符合题目要求的点gcd(x,y) = 1 那么第一段1<= y <= x ...
- bzoj 2818 gcd 线性欧拉函数
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1< ...
- bzoj 2190: [SDOI2008]仪仗队 线性欧拉函数
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为 ...
- 紫书 例题 10-26 UVa 11440(欧拉函数+数论)
这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...
- POJ2478(SummerTrainingDay04-E 欧拉函数)
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16927 Accepted: 6764 D ...
随机推荐
- iphone开发设置默认字体
It seems to be possible in iOS 5 using the UIAppearance proxy. [[UILabel appearance] setFont:[UIFont ...
- FIBON高精度
#include<stdio.h> #include<string.h> int u,n; ],b[],h[]; ],y[],z[]; int main() { char s( ...
- 制作新的train,test数据集
之前的数据集的train和test是直接按照网上下载的数据的前7000个作为训练集,后2212个作为测试集.看得出来,这个数据集是由开车录制视频转换来的图片数据,后面2000多个图片的场景和前面的场景 ...
- sql server 处理分母为空
SP 前面加下面设置,会忽略错误结果 直接返回null 不会导致SP 失败 SET ANSI_WARNINGS OFFSET ARITHABORT OFFSET ARITHIGNORE ON
- 【转】CPU个数,核心数,线程数
我们在买电脑的时候,经常会看cpu的参数,对cpu的描述有这几种:“双核”.“双核四线程”.“四核”.“四核四线程”.“四核8线程”……. 我们接触的电脑基本上都只有一个cup.cpu的个数很容易得到 ...
- Evaluate|GC content|Phred|BAC|heterozygous single nucleotide polymorphisms|estimate genome size|
(Evaluate):检查reads,可使用比对软件:使用SOAPaligner重新排列:采用massively parallel next-generation sequencing technol ...
- DP玄学优化——斜率优化
--以此博客来悼念我在\(QBXT\)懵逼的时光 \(rqy\; tql\) (日常%\(rqy\)) 概念及用途 斜率优化是\(DP\)的一种较为常用的优化(据说在高中课本里稍有提及),它可以用于优 ...
- initWithNibName:bundle awakeFromNib 区别
initWithNibName:bundle 定义:is a message sent to a view (or window) controller in order to create the ...
- 74个Swift标准库函数
74个Swift标准库函数 本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swi ...
- 使用Redis作为高速缓存
Redis适合哪些业务场景常规业务系统的数据库访问中,读写操作的比例一般在7/3到9/1,也就是说读操作远多于写操作,因此高并发系统设计里,通过NoSQL技术将热点数据(短期内变动概率小的数据)放入内 ...