UVA - 11327
UVA - 11327
https://vjudge.net/problem/28358/origin
求欧拉函数的前缀和,二分查找到那个位置,再从它开始暴力gcd找
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) using namespace std; long long n,cnt; long long a[N]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } long long gcd(long long a,long long b){ return a%b==0?b:gcd(b,a%b); } int main(){ a[1]=2; For(i,2,1e6) if(!a[i]) for(long long j=i;j<=1e6;j+=i){ if(!a[j]) a[j]=j; a[j]=a[j]/i*(i-1); } For(i,1,1e6) a[i]+=a[i-1]; while(cin>>cnt&&cnt){ n=lower_bound(a+1,a+1000000+1,cnt)-a; cnt-=a[n-1]; For(i,0,n){ if(gcd(i,n)==1) cnt--; if(!cnt){ o(i);p('/');o(n);p('\n'); break; } } } return 0; }