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

WA9 please help
Posted by genchildren 3 Jan 2017 22:37
#include <iostream>

using namespace std;
bool prime(long long n);


int main()
{
    long long a,odd, i, j, nice,b;
    cin >> a >> b;
    nice=0;
    if (a%2==1) {odd=a;} else {odd=a+1;}
    for (i=b; i>a; i--)
    {
       if (prime(i))
       {
           nice=i;
           break;
       }
    }

    if ((a==1 or nice==0) and b!=a) { cout << odd;}
    else if (b==a) {cout << a;}
    else
    if (nice) {cout << nice << endl;}
    return 0;
}

bool prime(long long n)
{
    bool w;
    w=n==2;
    if (!w and n%2)
    {
        w=1;
        for (int i=3; i*i<=n; i+=2)
        {
            if (n%i==0)
            {
                w=0 ;
                break;
            }
        }
    }
    if (w)
    {
        return 1;
    }
    else return 0;
}
Re: WA9 please help
Posted by ToadMonster 4 Jan 2017 13:48
Case "no prime is found" is suspicious.
You think answer is the minimal odd number in the interval. Could you prove it?
Why not maximal odd number in the interval?

Why you don't want to do try brute force?