493 - Rational Spiral

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
Ghost77 dimen
Learning poster
Posts: 67
Joined: Sun Sep 22, 2002 5:40 am
Location: Taiwan

493 - Rational Spiral

Post by Ghost77 dimen »

:oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops:
:oops:My sample test is :oops:
:oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops:

input
5000
2000
9999
output
13 / 64
41 / 2
91 / 10
And my max limit is 9999,
min one is 0.
I got W.A. at 1.6...seconds.
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

Your values are correct, try with a higher upper limit (my limit is 500000).
Ghost77 dimen
Learning poster
Posts: 67
Joined: Sun Sep 22, 2002 5:40 am
Location: Taiwan

Post by Ghost77 dimen »

Can you post some samples for me?

8) I want to rewrite the code, because it is slow when the max input data

is 500000.
Ghost77 dimen
Learning poster
Posts: 67
Joined: Sun Sep 22, 2002 5:40 am
Location: Taiwan

Post by Ghost77 dimen »

8) 8)
I got A.C. at 0.391 second, but I submitted it mpre than 10 times.

I am so frustrated that I am poor at programming.

But why the problem didn't point out the range? :evil:

By the way I found a interesting thing.

analyze as follows:Adrian Kuegel got A.C. ---limit 500000;

I got W.A. ----- array[500000]----limit 499999;

I got A.C. ----- array[1000000]-----limit 999999;

so that the max input is 500000.

How do Adrian Kuegel find the largest input ?
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

But why the problem didn't point out the range
I agree, it would be better if a range is given.
I got W.A. ----- array[500000]----limit 499999;
Sorry, my fault, I should have told you the exact limit I used (I checked if I have more than 500000 values only when I computed next row/column. Therefore my exact limit is 500071.
I have forgotten how I got this value, there are two possibilities: It was a hint in the algorithm field of that problem (I miss this field sometimes, it was really useful in such cases), or I tried several limits.
eloha
New poster
Posts: 38
Joined: Thu Oct 31, 2002 8:24 am
Location: Taiwan

493 Rational Spiral : No idea

Post by eloha »

Can anyone tell me how to solve 493 Rational Spiral ?
I have no idea at all. Thanx.
hujialie
New poster
Posts: 43
Joined: Thu Apr 17, 2003 10:24 am
Location: Shanghai,China

Post by hujialie »

Hello.

I have used 0.45sec and 4200KB memory to AC this problem
(using array[500000]).

Usually,when I got Crash(RunTime Error) for the sake of out of
range,I just check the table how many memory did those ACed
people use and calculate... :)

But I wonder how those who used 0.000sec and 64KB solve it?
Does anyone know a math method?
Retired from SJTU Accelerator 2004
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

I used more than a megabyte. :) Please, feel free to search the board about timing and memory usage issues. :) It's been explained again and again and again.... I just don't feel like telling the story once more.

Ivor
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

some comments + some test data

Post by Sedefcho »

I think this is a nice problem.

In my opinion it is not too easy and not too hard
at the same time.

I see there're only about 260 people who have solved it.
Why is that ? Judging from this fact one would say
it is a very difficult problem.

Here are some test cases for anyone who
is trying to solve it or will be trying to solve it.

Seems the Judge has some test case in which the input
number is bigger than 500 000.
Currently the Judge has no test case in which the
input number is bigger than 510 000.
My program has an upper limit ( for the input number
it can process ) of 510 000.


INPUT

Code: Select all

0
1
2
3
4
5
6
7
8
9
10
101
2000
2521
5000
9999
30101
499999
500000
509990
509999
510000 

OUTPUT

Code: Select all

1 / 1
0 / 1
-1 / 1
-2 / 1
2 / 1
1 / 2
-1 / 2
-3 / 2
-3 / 1
3 / 1
3 / 2
5 / 9
41 / 2
-46 / 25
13 / 64
91 / 10
-19 / 157
72 / 641
71 / 641
-374 / 647
-383 / 647
-384 / 647
Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski »

Hello, I got Accepted this problem but with very poor time (more than 7 sec).
Could anyone help me and tell how can I speed up it ?

I generate all values as in specification and use tree for eliminating duplicates. Is any faster algorithm ? From ranklist I hope that yes :) Maybe exist direct algorithm ? I know that most time-consuming parts of my algorithm are operations on the tree.

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

optimization

Post by Sedefcho »

Well, my ACC program is written in Java and runs for about 1.5 secs.
So I guess if I rewrite it in C++ it will run for about half of that time
( at least from the problems I have solved so far this is my impression
when comparing Java with C++ ). Sometimes that runtime ratio is even 1:3 or 1:5 ( talking about the same algorithm in C++ vs. Java ).

So if you your program is written in C++ the fact that it has a runtime
of 7 secs obviously means there's something in your algorithm which
is quite unefficient and could be optimized a lot.

I do not use trees I use just 4 large arrays. Which I think are used
exactly for eliminating doubles. Maybe that's where the speed up
comes from ( you use trees ).

So, Dominik, I can send you my code so that you compare
the algorithms on your own. It was a long time ago I solved
this one so I do not understand in details my algorithm ;)

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

Re: 493 Rational Spiral : No idea

Post by DD »

eloha wrote:Can anyone tell me how to solve 493 Rational Spiral ?
I have no idea at all. Thanx.
You can just directly simulate the whole process as problem defined, and prune the undesired rational spiral numbers.
The input limit and some test cases are already have some discussions in here (http://acm.uva.es/board/viewtopic.php?f ... acfa553906). :)
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.
L I M O N
Learning poster
Posts: 58
Joined: Wed Dec 31, 2003 8:43 am
Location: Dhaka, Bangladesh
Contact:

Re: 493 Rational Spiral : No idea

Post by L I M O N »

eloha wrote:Can anyone tell me how to solve 493 Rational Spiral ?
I have no idea at all. Thanx.
idea here : http://www.youngprogrammer.com
Hikari9
New poster
Posts: 20
Joined: Tue Jan 22, 2013 4:39 pm

Re: 493 wastes me a lot of time, and it haven't stopped.(SOS

Post by Hikari9 »

You can actually check for reoccurences of fractions using a 2D boolean array to eliminate the O(nlogn) tree checking.
(although make sure to simplify the fraction first :D)

Code: Select all

bool vis[1500][1500]; // will be able to hold for inputs upto 510000

...

// checking part
if( den!=0 && vis[num+750][den+750]==false ){ // +750 for offset on negative numbers
   vis[num+750][den+750] = true; // visit

   ... // add fraction to your list

}
Post Reply

Return to “Volume 4 (400-499)”