## 10015 - Joseph's Cousin

Moderator: Board moderators

sayem
New poster
Posts: 7
Joined: Sun Jul 12, 2009 10:30 pm

### 10015 - Joseph's Cousin

hey mf
i have use this input :
2 4 5 6 7 10 100 200 300 500 1000 2000 3000 3400 3501 0
and here is th output :
1
4
1
4
3
1
60
140
278
115
897
344
1613
2238
2326
what's the problem in my code? i can't detect it can u tell?
why is showing all 3?

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

### Re: 10015 - Joseph's Cousin

Most likely it's in your sieve() procedure. And it's too long for such a simple task.

Use debuggers to find what's wrong exactly.

vwxy825
New poster
Posts: 1
Joined: Mon Aug 24, 2009 6:57 am
Contact:

snape
New poster
Posts: 5
Joined: Sat Dec 03, 2011 4:18 pm

### uva problem no:10015

uva problem no:10015

still got WA,but dont know why?
here is my code:

#include<stdio.h>
#include<math.h>
#define max 3520
int p[max],ab[max],v[max],s,f,g,h,m,x,w,mm;

void sieve()
{
long i,j,c=0,x=0,b=0;
for(i=2;i<max;i++)
p=1;
for(i=2;i<=sqrt(max); )
{

for(j=i+i;j<=max;j+=i)
p[j]=0;
i++;
for(;!p;i++);
}
for(i=0;i<=max;i++)
{
if(p) //This 2 lines for checking the number of primes between 0 & 10000000.
{
ab=i;

b++;
}
}
mm=b;

}

void cutting(int o,int w)
{
int n,a[max],pi,ii,jj,f,k,g;
f=o;

while(f>1)
{
for(jj=0;jj<mm;jj++){
n=ab[jj];

if(n==0)
break;
while(n>f)
n=n-f;

n=n-1;
pi=n;

v[n]='\0';

n=n+1;

for(ii=0;n<=w;ii++)
{
a[ii]=v[n];

n++;
}
pi=pi-1;

for(k=0;k<=pi;k++)
{
while(v[k]=='\0')
k++;
if(k>pi)
break;
a[ii]=v[k];
ii++;
}

for(f=0;f<ii;f++)
v[f]=a[f];

w=w-1;

if(f==1)
break;

}
if(f==1)
break;
}
printf("%d\n",v[f]);

}

int main()
{

sieve();

while(1){
scanf("%ld",&s);
getchar();
if(s==0)
break;

for(f=1;f<=s;f++)
{
v[f-1]=f;
}
f=f-1;
w=s-1;
cutting(f,w);
}

return 0;

}

snape
New poster
Posts: 5
Joined: Sat Dec 03, 2011 4:18 pm

### uva problem no 10015

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: uva problem no:10015

Check input and AC output for thousands of problems on uDebug!

theemathas
New poster
Posts: 19
Joined: Mon Jan 07, 2013 9:30 am

### 10015 - Joseph's Cousin

got TLE
worst case test (1 2 3 ... to 3501 done by using commented code): around 30 sec (10 sec allowed)
I got this solved by a chat in uhunt
code deleted after AC to prevent copies!
Last edited by theemathas on Thu Jan 10, 2013 7:48 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 10015 - Joseph's Cousin

Check input and AC output for thousands of problems on uDebug!

DD
Experienced poster
Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California
Contact:

### Re: 10015 - Joseph's Cousin

Even I used offline-built table to solve this, but I still noticed that there are people can solve this problem using 0.000 secs
Have you ever...
• Wanted to work at best companies?
• Struggled with interview problems that could be solved in 15 minutes?
• Wished you could study real-world problems?
If so, you need to read Elements of Programming Interviews.