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 1023. Buttons

I've got AC, but in 1.968 second, please help me!!!!
Posted by Cyclops 20 Aug 2008 18:53
Here is my code, what can make my code run faster? Please check it up...

#include <iostream>
#include <cmath>
using namespace std;
bool prime(long long x)
{
    int i;

    if(x==2)
       return true;
    else if(x%2==0)
       return false;

    for(i=3; i*i<=x; i+=2)
        if(x%i==0)
           return false;
    return true;

}
int main()
{
    long long K, s;

    cin>>K;
    if(prime(K)==true)
    {
        cout<<K-1;
        return 0;
    }
    else
    {
        for(long long i=3; i<499999950; i++)
        {
            if(K%i==0)
            {
                cout<<i-1;
                return 0;
            }
        }
    }
    return 0;
}
Re: I've got AC, but in 1.968 second, please help me!!!!
Posted by Tolya_wolf 21 Mar 2010 04:10
Функция Prime может быть встроена в main

Вместо
if(Prime(K)==true)
можно писать
if(Prime(K))
Re: I've got AC, but in 1.968 second, please help me!!!!
Posted by † Ленин † [Yaroslavl SU] 21 Mar 2010 06:24
oh my... =)

This lazy code gets AC in 0.562s
#include <cstdio>
int n,r;
int main()
{
.scanf("%d", &n);
.r=2;
.while ( n % (r+1) != 0 )
..++r;
.printf("%d",r);
.return 0;
}

And this a little bit (only a little) improved, especially for you, gets AC in 0.296s.
#include <cstdio>
int n,r;
int main()
{
.scanf("%d", &n);
.if ( n % 3 == 0 )
.printf("2");
.else if ( n % 4 == 0 )
..printf("3");
.else
.{
..r=4;
..while ( n % ++r != 0 )
...++r;
..printf("%d",r-1);
.}
.return 0;
}