100 - The 3n + 1 problem

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

Moderator: Board moderators

Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

I recently read a post in this volume about this prob. I've checked also the problemstat and to my astonishment this prob can be solved with all the numbers in CPU time ZERO !!! :o :o
How did they do that????
Anyone care to share special tricks, hints, or maybe even algo??
Thanx !
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

prob 100 in 0.000 sec WOWWW!!!

Post by Joseph Kurniawan »

Actually I've posted this in the sticky post, but got no replies. I'm just wondering the way people got AC in such short time. Anyone care to share some special tricks, hints or maybe even algo??
Thanx in advance!! :D :D :D :D
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
Cosmin.ro
Learning poster
Posts: 95
Joined: Thu Aug 21, 2003 12:02 am

Post by Cosmin.ro »

You can use memoization for fast calculating every cycle length for n=1 to 1000000. The number of intervals in wich the result is asked is smaller than 1000, so you can store the intervals and sort their endpoints. In O(n) you can get the maximum value in each interval (xi,xi+1). Now you can get the answer for each query interval in O(n) if you scan the intervals in the sorted order.
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

Thnx for the tip !!!!
I'll try that asap!! :D :D :D :D
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
bignad98
New poster
Posts: 6
Joined: Mon Sep 15, 2003 3:21 am
Location: Charleston, S.C.

Problems 100, 118 -- terminating standard input, C++

Post by bignad98 »

I don't know an elegant way to control a loop based on reaching the end-of-file of the standard input. I think this is the reason I got consistent WA's on problem 100, so I gave up until the same thing comes up on 118.

For 100 i was doing

int firstnum, lastnum;
while( cin>>firstnum>>lastnum)
:evil:
but the loop never ends until i enter a non-integer value, and i think the judge wants something else. is there any other way besides reading the input stream character by character or line by line and breaking it into tokens?

(i'm a mostly self-taught novice, forgive me)
"Moral victories are for girls!" -- Shawn Kiernan

"You can't spell geek without EE..." -- Larkin Gentry
bugzpodder
Experienced poster
Posts: 147
Joined: Fri Jun 13, 2003 10:46 pm

Post by bugzpodder »

what you did was fine. if your code ran more than 10 seconds, most likely its either your algorithm is too slow, or that you have an infinite loop somewhere
Mage
New poster
Posts: 2
Joined: Thu Sep 18, 2003 9:05 am

WA in problem number 100

Post by Mage »

I don't understand with the cycle_length -> between integer i and j, what is this mean?
Please tell me if anyone know about it. Thanks :D
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

For the num 22, we get the sequence of 16 numbers:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
thus the cycle length for 22 is 16.
So for the prob, you're asked to count the max cycle between the range i to j.
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
OmShanti
New poster
Posts: 2
Joined: Wed Sep 24, 2003 6:51 pm
Location: Gdynia [Poland]

Always WA in 100 [Pascal]

Post by OmShanti »

I don't know what is wrong:[pascal]program program100;
const
Niesk=0;
var
tab: array[1..1000000] of qword;

i,j,k,h,p: longint;
max,printed: qword;
function oblicz(num: longint): qword;
var
p: qword;
begin
if tab[num]>niesk then
p:=tab[num]
else
if (num mod 2) = 1 then
p:= oblicz(3*num + 1)+1
else
p:= oblicz(num div 2)+1;
{ writeln(num);}
tab[num]:=p;
oblicz:=p;
end;

begin
for i:=1 to 1000000 do
tab:= niesk;
tab[1]:=1;
while not(eof) do
begin
max:=0;
readln(i,j);
p:=0;
if i>j then
begin
h:=i;
i:=j;
j:=h;
p:=1;
end;
for k:=i to j do
begin
{ writeln('****');
writeln('*',k:2,'*');
writeln('****');}
printed:= oblicz(k);
{ writeln(printed);}
if printed>max then
max:=printed;
end;
if p=0 then
writeln(i,' ',j,' ',max)
else
writeln(j,' ',i,' ',max);
end;
end.
[/pascal]
Ivor
Experienced poster
Posts: 150
Joined: Wed Dec 26, 2001 2:00 am
Location: Tallinn, Estonia

Post by Ivor »

you must not change the order of the input values...
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.
OmShanti
New poster
Posts: 2
Joined: Wed Sep 24, 2003 6:51 pm
Location: Gdynia [Poland]

Post by OmShanti »

I don't:[pascal]p:=0;
if i>j then
begin
h:=i;
i:=j;
j:=h;
p:=1;
end;
...
if p=0 then
writeln(i,' ',j,' ',max)
else
writeln(j,' ',i,' ',max); {the order was changed earlier}[/pascal]
Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

question about 100

Post by Eduard »

please answer to my question if you you solve the problem 100.
Du you solve this program only whith longint?i whant to say that when i give 133381 number to my problem after some time he got a number bigen then maximum longint.Do you solve it using array, to get big numbers?or...
please answer :roll: :roll:
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten »

I solved the problem using "long long" data type in C++.. not sure if it's really necessary though
Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten »

I know nothing about pascal, but i suggest you do the input/output as follows (pseudocode):

Code: Select all

read i, j;
write i, j;
if( i< j ) swap i,j;
compute max
write max
Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten »

also, please give your variables english names, it makes your code easier to understand for us...
I think your code doesn't handle correctly the case that your sequence is bigger than 10^6, for example
999999 2999998 1499999 etc.
Post Reply

Return to “Volume 1 (100-199)”