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

AC in 0.015
Posted by Aditya Singh 15 Apr 2017 05:24
#include<bits/stdc++.h>
using namespace std;
bool B[1000008];
float check(long N)
{
    long K=sqrt(N);
    long D=1;
    for(long i=2;i<=K;i++)
        if(N%i==0)
            D+=(i+N/i);
    return (D/(float)(N));
}
void prime()
{
    for(long i=2;i<=1000;i++)
        if(B[i]==0)
            for(long j=2;i*j<=1000000;j++)
                B[i*j]=1;
}
int main()
{
    ifstream cin("input.in");
    ofstream cout("output.out");
    long X,Y;
    cin>>X>>Y;
    prime();
    if(X==1)
        cout<<1;
    else{
        bool flag=0;
        for(long i=Y;i>=X;i--)
            if(B[i]==0){
                cout<<i;
                flag=1;
                break;
            }
        if(!flag){
            float P=2e9;
            long pos;
            for(long i=X;i<=Y;i++)
            {
                float H=check(i);
                if(H<P){
                    pos=i;
                    P=H;
                }
            }
        cout<<pos;
        }
    }
}
Re: AC in 0.015
Posted by Igor Kulatov 10 Feb 2018 15:29
Hi,

Very good solution! Just one note, it doesn't affect AC, but:

In D+=(i+N/i); if (i == N/i) you don't need to add both i and N/i, but just i.

In this case result of check(N) will be 100% equal to triviality(N) in all cases.