406 - Prime Cuts
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 406 - WA ...
That looks correct but on ideone your code prints:
https://ideone.com/RFTlgB
Try modifying your code to handle this case correctly.
https://ideone.com/RFTlgB
Try modifying your code to handle this case correctly.
Check input and AC output for thousands of problems on uDebug!
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
-
- New poster
- Posts: 5
- Joined: Sun Apr 08, 2012 5:36 pm
Why TLE?
#include<cstdio>
#include<cmath>
#define max 1004
int prime[max+1];
int twin[max];
void sieve()
{
int i,j;
for(i=2; i <= max; i++)
prime = 1;
for(i=2; i <= sqrt(max);)
{
for(j = 2*i; j <= max; j += i)
{
prime[j]=0;
}
i++;
for(; !prime; i++);
}
prime[1] = 1;
prime[2] = 1;
prime[0] = 0;
}
int main()
{
sieve();
int n,m,p = 0,k;
while(scanf("%d%d",&n,&m))
{
k = p = 0;
printf("%d %d: ",n,m);
for(int i = 1; i <= n; i++)
{
if(prime == 1)
{
k++;
twin[++p] = i;
}
}
if(m*2 >= k)
{
for(int r = 1; r <= n; r++)
{
if(prime[r] == 1)
printf("%d ",r);
}
}
else if(k % 2 == 0)
{
for(int i = k/2-m+1,j = 0; j < m*2; i++)
{
printf("%d ",twin);
j++;
}
}
else if(k % 2 != 0)
{
for(int i = k/2-m+2,j = 0; j < m*2-1; i++)
{
printf("%d ",twin);
j++;
}
}
printf("\n\n");
}
return 0;
}
#include<cmath>
#define max 1004
int prime[max+1];
int twin[max];
void sieve()
{
int i,j;
for(i=2; i <= max; i++)
prime = 1;
for(i=2; i <= sqrt(max);)
{
for(j = 2*i; j <= max; j += i)
{
prime[j]=0;
}
i++;
for(; !prime; i++);
}
prime[1] = 1;
prime[2] = 1;
prime[0] = 0;
}
int main()
{
sieve();
int n,m,p = 0,k;
while(scanf("%d%d",&n,&m))
{
k = p = 0;
printf("%d %d: ",n,m);
for(int i = 1; i <= n; i++)
{
if(prime == 1)
{
k++;
twin[++p] = i;
}
}
if(m*2 >= k)
{
for(int r = 1; r <= n; r++)
{
if(prime[r] == 1)
printf("%d ",r);
}
}
else if(k % 2 == 0)
{
for(int i = k/2-m+1,j = 0; j < m*2; i++)
{
printf("%d ",twin);
j++;
}
}
else if(k % 2 != 0)
{
for(int i = k/2-m+2,j = 0; j < m*2-1; i++)
{
printf("%d ",twin);
j++;
}
}
printf("\n\n");
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 406 - WA ...
change line 30 to:
while(scanf("%d%d",&n,&m)==2)
while(scanf("%d%d",&n,&m)==2)
Check input and AC output for thousands of problems on uDebug!
406 - Prime Cuts WA
I have got many WAs with this problem 406.....
I test many test cases , but I still get WA lol...
Please help me
The following is my C++ code :
I test many test cases , but I still get WA lol...
Please help me
![:(](./images/smilies/icon_frown.gif)
The following is my C++ code :
Code: Select all
The deleted code is a AC code!
Last edited by 123321321 on Sat Mar 30, 2013 11:34 am, edited 2 times in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 406 - Prime Cuts WA
Try input 1000 1000
Check input and AC output for thousands of problems on uDebug!
Re: 406 - Prime Cuts WA
I have tried this case:brianfry713 wrote:Try input 1000 1000
input:
Code: Select all
1000 1000
output:
Code: Select all
1000 1000: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997
Re: 406 - Prime Cuts WA
123321321
1st for input 1000 1000 your code don't give any output in my Computer And
Try this input
May be it's Help you ....
1st for input 1000 1000 your code don't give any output in my Computer And
Try this input
Code: Select all
997 997
997 991
Code: Select all
enjoying life .....
Re: 406 - Prime Cuts WA
Thanks a lot! I finally get AC!shuvokr wrote:123321321
1st for input 1000 1000 your code don't give any output in my Computer And
Try this inputMay be it's Help you ....Code: Select all
997 997 997 991
The main problem is that I originally use IDE code block to write the c++ code. In the IDE, input "1000 1000" can correctly produce output. Then I copy the code to VS 2010, The output is empty !! It's like what you say! So I debug it on VS2010~
Re: 406 - Prime Cuts WA
getting WA. need help.
Code deleted after getting AC
Code deleted after getting AC
Last edited by ashdboss on Wed Jul 03, 2013 10:59 am, edited 1 time in total.
Re: 406 - Prime Cuts WA
try this-
input:
input:
correct output:13 6
13 6: 1 2 3 5 7 11 13
Re: 406 - Prime Cuts WA
thanks t.tahasin...got AC... ![:)](./images/smilies/icon_smile.gif)
![:)](./images/smilies/icon_smile.gif)
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 406 - Prime Cuts WA
Be careful, there is a case where C > N in the judge's input.
Check input and AC output for thousands of problems on uDebug!
Re: 406 - Prime Cuts WA
what's wrong ... i get many wa..
Code: Select all
//406 - Prime Cuts
#include <iostream>
#include <cstdio>
using namespace std;
int is_prime(int n);
int main()
{
//freopen("uvainput.txt", "r", stdin);
//freopen("uvaoutput.txt", "w", stdout);
int i,j,n,c,l,show,start,k=0;
int a[10000];
while(scanf("%d %d",&n,&c)==2)
{
if(k)
printf("\n");
j=0;
for(i=1; i<=n; i++)
{
if(is_prime(i))
{
a[j]=i;
j++;
}
}
l=j;
if(l%2==0)
show=c*2;
else
show=(c*2)-1;
start=(l-show)/2;
printf("%d %d: ",n,c);
if(show>l)
{
for(i=0; i<l; i++)
{
printf("%d ",a[i]);
}
}
else
{
for(i=start,j=0; j<show; i++,j++)
{
printf("%d ",a[i]);
}
}
printf("\n");
k++;
}
return 0;
}
int is_prime(int n)
{
int d;
if(n<1)
return 0;
if(n==1) //1 considerd as prime
return 1;
for (d=2; d*d<=n; d++)
{
if(n%d==0)
return 0;
}
return 1;
}