ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1118. Nontrivial Numbers

WHY WA on 1118!!!!!!!
Posted by michel mizrahi 4 Apr 2005 05:10
I get too many WA with this problem (1118). In test number 3
I just use a simple algorithm, if lower number is 1 then print 1
else go in a loop from j to l, I mean from the max number until the min. number and start to calculate the nontrivial number of every number until I find a prime, if I find a prime then I print the least nontrivial number until this number (including the first prime that I found)
here is my code:

#include <stdio.h>
#include <math.h>
double s,res=2147483647;
int r=1,rs;

calc(int n,int p){
    int i,k=1;
    p++;
    for(i=0;i<p;i++) k*=n;
    k=(k-1)/(n-1);
    r*=k;
}

fact(int n){
    int p=0,i=3,l=sqrt(n)+1;
    double t=n;
    while(n%2==0){
         n/=2;
         p++;
    }
    if(p>0){
        calc(2,p);
        p=0;
    }
  while(i<l){
     while(n%i==0){
         n/=i;
         p++;
     }
     if(p>0){
         calc(i,p);
         p=0;
     }
     i+=2;
  }
    if(n>1) calc(n,1);
    r-=t;
    s=r/t;
    if(s<res){
        rs=t;
        res=s;
    }
  r=1;
}

is_prime(n){
    int i=3,l=sqrt(n)+1;
    if(n%2==0) return 0;
    for(i=3;i<l;i+=2)
     if(n%i==0) return 0;
     return 1;
}

int main(){
    int l,i,j;
    scanf("%d %d",&l,&j);
    if(l==1) printf("1\n");
    else{
     for(i=j;i>=l;i--) {
       fact(i);
       if(is_prime(i)) break;
   }
  printf("%d",rs,res);
  }
  return 0;
}

if someone can help me I would appreciate very much!!
thanks
Re: WHY WA on 1118!!!!!!!
Posted by Cybernetics Team 4 Apr 2005 14:38
I search the greatest prime in the interval. If there are not any primes then we go brute force