According to the p*a<=100000,a/b<=p/q<=1, so p<=q, because 1<=a,b<=1000, so 1/1000<=p/q
p>=q/1000, and because of p*q<=100000, so q*q<=10^8,q<t=sqrt (10^8) = 10000, so just play 10000 or less of the prime table, and then add a little pruning can be, In the first 100000 of a dozen prime tables, tle.
Code:
#include <iostream> #include <cstdio> #include <memory.h> #include <math.h> #include < Algorithm> using namespace std; BOOL flag[100005]; int prime[100000]={0}; int m,a,b,cnt; Double S; void Get_prime () {int i,j,t; memset (flag,0,sizeof (flag)) t= (int) sqrt (double) 10000); prime[0]=2 cnt=1; for (i=3;i< =t;i+=2) {if (!flag[i]) {prime[cnt++]=i;} for (J=i+i;j<=10000;j+=i) flag[j]=true;} if (t%2==0) t++; for (i=t;i<=10000;i+=2) {if (!flag[i]) {prime[cnt++]=i}}}} int main () {int i,j,k,x,y,t; Get_prime () while (scanf ("%d%d%d", &m,&a,&b)!=eof) {if (m==0&&a==0 &&b==0) break; t=0; S= (double) A/double b; for (i=cnt-1;i>=0;i--) {if (prime[i]>m) continue, for (j=i;j<cnt;j++) {if (prime[j]>m) break; if (prime[i]* prime[j]>m)//j increment, prime[j] also increase the break; Double k= (double) prime[i]/(double) prime[j];//prime[j] increment, quotient descending if (k<s) break; if (prime[i]*prime[j]>t) {x=prime[i]; y=prime[j]; t=x*y;}} } cout<<x<< "" <<y<<endl; return 0; }