|
|
back to boardI'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!!!! Функция Prime может быть встроена в main Вместо if(Prime(K)==true) можно писать if(Prime(K)) Re: I've got AC, but in 1.968 second, please help me!!!! 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; } |
|
|