3478 The title meaning is that the Farey sequence consists of a series of fractional A/b (0<a<b<=n and gcd (b) =1) that cannot be numerator in ascending order, and then gives the first few items of FN, allowing you to calculate how many fractions are in FN.
It is obvious that the number of FN is the number of coprime and (2,3,4.....n-1,n) respectively. That is, the first n of the Euler function is obtained.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <queue>6#include <map>7#include <algorithm>8#include <vector>9 Ten using namespacestd; One A Const intMAXN =1000000; - -typedefLong LongLL; the -LL phi[maxn+1]; - - voidInit () + { - LL i,j; + for(i=1; i<=maxn;i++) Phi[i] =i; A for(i=2; i<=maxn;i+=2) phi[i]/=2; at for(i=3; i<=maxn;i+=2){ - if(Phi[i] = =i) { - for(j=i;j<=maxn;j+=i) { -PHI[J] = phi[j]/i* (i-1); - } - } in } - for(i=3; i<maxn+1; i++){ tophi[i]+=phi[i-1]; + } - } the * intMain () $ {Panax Notoginseng init (); - intN; the while(cin>>n&&N) { +printf"%lld\n", Phi[n]); A } the + return 0; -}View Code
3090 of the situation is similar to 3478, but to note that the point of view is the x=y of this straight line symmetry, so the final answer is 2*phi[n]+1,x=y also counted.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <queue>6#include <map>7#include <algorithm>8#include <vector>9 Ten using namespacestd; One A Const intMAXN = +; - -typedefLong LongLL; the -LL phi[maxn+1]; - - voidInit () + { - LL i,j; + for(i=1; i<=maxn;i++) Phi[i] =i; A for(i=2; i<=maxn;i+=2) phi[i]/=2; at for(i=3; i<=maxn;i+=2){ - if(Phi[i] = =i) { - for(j=i;j<=maxn;j+=i) { -PHI[J] = phi[j]/i* (i-1); - } - } in } - for(i=2; i<maxn+1; i++){ tophi[i]+=phi[i-1]; + } - } the * intMain () $ {Panax Notoginseng init (); - intN; the intCAs; +Cin>>CAs; A for(intI=1; i<=cas;i++){ theCin>>N; +printf"%d%d%d\n", I,n,2*phi[n]+1); - } $ $ return 0; -}View Code
POJ 3478 POJ 3090 (Application of Euler functions)