poj2478 欧拉函数水题
poj2478 欧拉函数水题
Description
The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
You task is to calculate the number of terms in the Farey sequence Fn.
F2 = {1/2}
F3 = {1/3, 1/2, 2/3}
F4 = {1/4, 1/3, 1/2, 2/3, 3/4}
F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
You task is to calculate the number of terms in the Farey sequence Fn.
Input
There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 10 6). There are no blank lines between cases. A line with a single 0 terminates the input.
Output
For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn.
Sample Input
2 3 4 5 0
Sample Output
1 3 5 9
题意:如题,给定n,求1~n的范围内互质数对的数目。
思路:欧拉函数水题。显然,小于n且与n互质的数即为欧拉函数,累积求和即为题中所要的答案。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<string> #include<math.h> #include<cctype> #define ll long long #define REP(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) #define REPP(i,a,b,t) for(int (i)=(a);(i)<=(b);(i)+=(t)) #define PII pair<int,int> #define MP make_pair #define PB push_back #define RI(x) scanf("%d",&(x)) #define RLL(x) scanf("%lld",&(x)) #define RI64(x) scanf("%I64d",&(x)) #define DRI(x) int x;scanf("%d",&(x)) #define DRLL(x) ll x;scanf("%lld",&(x)) #define DRI64(x) llx;scanf("%I64d",&(x)) #define MS0(a) memset((a),0,sizeof((a))) #define MS1(a) memset((a),0,sizeof((a))) #define MS(a,b) memset((a),(b),sizeof((a))) using namespace std; const int maxn=4000100; const int INF=(1<<29); const double EPS=0.0000000001; const double Pi=acos(-1.0); int euler[maxn]; ll f[maxn]; ll n; void getEuler() { MS0(euler); euler[1]=0; REP(i,2,maxn-1){ if(!euler[i]){ REPP(j,i,maxn-1,i){ if(!euler[j]) euler[j]=j; euler[j]=euler[j]/i*(i-1); } } } } int main() { getEuler(); MS0(f); REP(i,1,maxn-1) f[i]=f[i-1]+euler[i]; while(cin>>n,n>0){ cout<<f[n]<<endl; } return 0; }
没有AC不了的题,只有不努力的ACMER!