10120 - Gift?!
Moderator: Board moderators
10120
I can solve it by using a recursion. But my program has TIME LIMIT EXCEEDED. Know anybody a better(faster) solution?
10120 - Gift?!
Can anyone tell me what is wrong?
I got Time Limit Exceeded.
[c]#include <stdio.h>
char GotGift;
long N,M;
void jump(long p,long i)
{
if (p==M)
{ GotGift=1; return; }
if (p>N || p<1)
return;
jump(p+(2*i-1),i+1);
if (!GotGift)
jump(p-(2*i-1),i+1);
}
main()
{
while (1)
{
scanf("%ld %ld",&N,&M);
if(N==0 && M==0) break;
GotGift=0;
jump(1,2);
if (GotGift) printf("Let me try!\n");
else printf("Don't make fun of me!\n");
}
return 0;
}
[/c]
I got Time Limit Exceeded.
[c]#include <stdio.h>
char GotGift;
long N,M;
void jump(long p,long i)
{
if (p==M)
{ GotGift=1; return; }
if (p>N || p<1)
return;
jump(p+(2*i-1),i+1);
if (!GotGift)
jump(p-(2*i-1),i+1);
}
main()
{
while (1)
{
scanf("%ld %ld",&N,&M);
if(N==0 && M==0) break;
GotGift=0;
jump(1,2);
if (GotGift) printf("Let me try!\n");
else printf("Don't make fun of me!\n");
}
return 0;
}
[/c]
-
- Learning poster
- Posts: 67
- Joined: Sun Sep 22, 2002 5:40 am
- Location: Taiwan
-
- Learning poster
- Posts: 67
- Joined: Sun Sep 22, 2002 5:40 am
- Location: Taiwan
-
- Experienced poster
- Posts: 183
- Joined: Thu Nov 11, 2004 12:35 pm
- Location: AIUB, Bangladesh
10120
![:(](./images/smilies/icon_frown.gif)
Code: Select all
#include<stdio.h>
void main()
{
int m,n,i,p[100000],x,y,count,flage;
while(scanf("%d%d",&n,&m) && m && n)
{
if(n>=49 || m==1)
printf("Let me try!\n");
else
{
count=1;
p[count++]=1;
flage=0;
for(i=1;i<count;i++)
{
x=p[i]-2*(i+1)+1;
y=p[i]+2*(i+1)-1;
if(x<=n && x>=0)
{
if(x==m)
{
printf("Let me try!\n");
flage=1;
break;
}
else
{
p[count++]=x;
}
}
else if(y<=n && y>=0)
{
if(y==m)
{
printf("Let me try!\n");
flage=1;
break;
}
else
{
p[count++]=y;
}
}
else
{
break;
}
}
if(!flage)
printf("Don't make fun of me!\n");
}
}
}
Jalal : AIUB SPARKS
-
- Experienced poster
- Posts: 183
- Joined: Thu Nov 11, 2004 12:35 pm
- Location: AIUB, Bangladesh
Hi ,I changed it in recursive method, still wrong answer.....
![:(](./images/smilies/icon_frown.gif)
Code: Select all
#include<stdio.h>
int n,m;
int check(int v,int i)
{
int x,y;
x=v+2*i-1;
y=v-2*i+1;
if(x==m || y==m)
return 1;
if(x>0 && x<=n)
{
if(check(x,i+1))
return 1;
else
return 0;
}
else if(y>0 && y<=n)
{
if(check(y,i+1))
return 1;
else
return 0;
}
return 0;
}
void main()
{
while(scanf("%d%d",&n,&m) && m && n)
{
if(n>50 || m==1)
printf("Let me try!\n");
else
{
if(check(0,1))
printf("Let me try!\n");
else
printf("Don't make fun of me!\n");
}
}
}
Jalal : AIUB SPARKS
10120 Gift
i think this problem can be solved by using bfs. but my following code give wa. can anyone help? my idea is wrong or i just made wrong implementation of bfs?
// code deleted after AC
// code deleted after AC
Last edited by taskin on Sat Sep 02, 2006 8:16 am, edited 1 time in total.
-
- Experienced poster
- Posts: 183
- Joined: Thu Nov 11, 2004 12:35 pm
- Location: AIUB, Bangladesh
Hi, your code has some problem.
consider the case: 49 49 , surely it is reachable.
1+3+5+7+9+11+13 =49
the fact is that, there can be 2 nodes in the same level of your bfs tree, but you don't have any information of that, u just consider them as they are in different level.
Input:
output:
consider the case: 49 49 , surely it is reachable.
1+3+5+7+9+11+13 =49
the fact is that, there can be 2 nodes in the same level of your bfs tree, but you don't have any information of that, u just consider them as they are in different level.
Input:
Code: Select all
1000000 999999
1000000 10
1000000 6
101 101
33 33
19 19
1000000 1
1000000 2
1000000 3
1000000 4
1000000 5
1000000 7
1000000 8
1000000 9
1000000 1000000
1000000 900000
1000000 111111
123456 123456
707 49
707 505
21 20
125 124
111111 2222
16 15
12 2
49 49
10 1
9 5
303030 5064
17 17
808805 47566
0 0
output:
Code: Select all
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Don't make fun of me!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Let me try!
Don't make fun of me!
Let me try!
Let me try!
Let me try!
Don't make fun of me!
Let me try!
Don't make fun of me!
Let me try!
Jalal : AIUB SPARKS