Uva--10820(数论,欧拉函数,筛法)
2014-09-03 00:58:22
原题题目老长,不多说了,小白书例题。
思路:1 <= x,y <= n,要求(x,y)的对数,所以x,y互质。不妨先设x > y,对于某个给定的x,(x,y)的对数就是其欧拉函数值。所以把1~n的所有欧拉函数值求出来 × 2就行了(因为先设定x>y,所以×2),最后要减去1(因为(1,1)算重了一次)
1 /************************************************************************* 2 > File Name: Uva10820.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 03 Sep 2014 12:25:06 AM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 typedef long long ll; 16 const int RA = 50000; 17 18 int N; 19 int phi[RA + 5]; 20 ll sum[RA + 5]; 21 22 void Phi_table(){ 23 phi[1] = 1; 24 for(int i = 2; i <= RA; ++i) if(!phi[i]) 25 for(int j = i; j <= RA; j += i){ 26 if(!phi[j]) phi[j] = j; 27 phi[j] = phi[j] / i * (i - 1); 28 } 29 } 30 31 void Pre(){ 32 for(int i = 1; i <= RA; ++i) 33 sum[i] = sum[i - 1] + (ll)phi[i]; 34 } 35 36 int main(){ 37 Phi_table(); 38 Pre(); 39 while(scanf("%d",&N) != EOF && N){ 40 printf("%lld\n",sum[N] + sum[N] - 1); 41 } 42 return 0; 43 }