标签:
Description
Input
Output
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; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4570588.html