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

shakil
Learning poster
Posts: 74
Joined: Sat Jul 15, 2006 6:28 am
Location: CUET , bangladesh
Contact:

100 ???

Post by shakil »

i solved 100 but it took 4 s. How it can solved in 0 s ???
Please help......
SHAKIL
Itachi-Forsaken
New poster
Posts: 10
Joined: Mon May 07, 2007 4:45 am

Post by Itachi-Forsaken »

Hey to all of you programming pros out there can someone plz tell me what is wrong with my program I always get Compiler Error for some reason and I have no idea what is wrong with this because it works fine on my computer GRR

#include<iostream>

#define MAXSIZE 100000
using namespace std;

int find_length(int num)
{
if(num==1) return 1;
int n=num;
if(n%2==0) n/=2;
else n=n*3+1;
return find_length(n)+1;
}

int main()
{
long min, max;

while(cin>>min>>max)
{
cout<<min<<" "<<max<<" ";
if(min>max)
{
long a=min; min=max; max=a;
}
long max_length=0;
for(long i=min; i<=max; i++)
{
long n=find_length(i);
if(n>max_length)
max_length=n;
}
cout<<max_length<<endl;

}
return 0;
}

Thanks for helping

btw sometimes it says "received". What does that mean??
Rocky
Experienced poster
Posts: 124
Joined: Thu Oct 14, 2004 9:05 am

Post by Rocky »

now its ok ...i send it by pm check it!!!

GOOD LUCK
Rocky
Itachi-Forsaken
New poster
Posts: 10
Joined: Mon May 07, 2007 4:45 am

Post by Itachi-Forsaken »

Thanks, but it always says received instead of accepted for some reason =S
What is it suppose to mean?

edit: nvm, it works now =D thanks a lot
Rocky
Experienced poster
Posts: 124
Joined: Thu Oct 14, 2004 9:05 am

Post by Rocky »

every code at first says recieved at first but after refresh and after some time it give it answer....that accepted/wa/ce...etc...
so wait for some time and refresh the judge page yeah..u can check ur statics also after some time..

GOOD LUCK
Rocky
nachopol
New poster
Posts: 2
Joined: Wed May 16, 2007 10:12 am

100 - Time Exceeded (?)

Post by nachopol »

Hi. I've received a time exceeded (10.025 seconds) when in other computers, the program takes 0 seconds. Could it be the entrance mode? Here is the code. Thanks in advance:
#include "stdio.h"
void main(){
long n,i,x,n0,n1,mc,c=1;
while (scanf("%ld %ld",&n0,&n1)) {
printf ("%ld %ld ",n0,n1);
mc=1;
if (n0>n1) {x=n0;n0=n1;n1=x;}
for (i=n0;i<=n1;i++) {

c=1;
n=i;
while(n>1){
n=(n%2)?(3*n+1):(n/2);
c++;
}
if (c>mc) mc=c;
}
printf ("%ld\n",mc);
}
}
Schultz
New poster
Posts: 13
Joined: Wed May 16, 2007 12:39 am

100 - 3n+1 - Memorisation

Post by Schultz »

I have solved the 3n+1 problem by use of the memorisation method. In my computer, it solves the input "1 999999" 60 times faster than brute force. But when I submit it, besides getting AC, it takes over a second to get it correct, considering that the brute force took 3 seconds. Furthermore, I expected the 4000000 elements memorisation table to be built once only, taking no more than 0.1 seconds, but I am strongly suspecting my program is being loaded up more than once.

Finally, I want to ask how many times does the judge call my main function, i.e. how many times does it start up my program. Until today I thought it was once only but I am not sure it is.

I would post the code, but it is AC.

Thanks in advance.
nachopol
New poster
Posts: 2
Joined: Wed May 16, 2007 10:12 am

EOF

Post by nachopol »

The problem was in the loop. It should be:
while (scanf("%ld %ld",&n0,&n1)!=EOF)
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Search the board first. Don't open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Use existing threads.
Ami ekhono shopno dekhi...
HomePage
SARKAR
New poster
Posts: 21
Joined: Tue May 22, 2007 4:18 pm

Post by SARKAR »

use memorization

firstly it took 6 sec to solve the problem
using memorization i solved it in 0.06

dont worry about 0.000

ani_mitr86
New poster
Posts: 20
Joined: Mon May 28, 2007 4:36 pm
Location: India

suggestion to Notta Benna

Post by ani_mitr86 »

To Notta Benna

you are taking only one set of input and printing out the answer

you have to take the input sets in a loop till the end of file.
you can read in one set of input , print out the output then again read in next set....
go on till there is no more input
diptesh_chatterjee
New poster
Posts: 1
Joined: Sun May 20, 2007 7:18 am
Location: kharagpur

MEMORIZATION

Post by diptesh_chatterjee »

I am new to this world of programming . Can someone tell me what memorization is?? I got a TLE error when the judge executed my code
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post by mmonish »

>>diptesh_chatterjee
I didn't do any kind of memorization & i got AC in 3.393 sec.

Here u can store the chain length. u must not calculate the chain length for a number twice.
here is an example
let i = 3 & j = 6
then first step u calculate the chain length for 3 is 8.
chain length for 4 is 3.
chain length for 5 ->16 ->8 ->4(add cycle length of 4)need no further calculation.
chain length for 6->3(add cycle length of 3) only one operation need.

Hope it helps.
MeNFolkS
New poster
Posts: 1
Joined: Sun Jun 03, 2007 5:28 pm

How to terminate the input

Post by MeNFolkS »

I'v been tryin out problem number 100, n the problem i'v been havin is in terminating the input.....
problems statement says tht each test case would be in a new line...like
10 100
20 200

nw how to terminate tht....

i thought this would work

while ( !feof ( stdin ) )
{
cin >> a; cin >> b;
//process
}

but this code is givin me run-time exceed error,

so i tried out the desi version

while ( scanf ( %d %d ), &a, &b ) != EOF )
{
//process
}

n this method works..


so nw wht is the correct way of terminating the input, what if i want the design to be like

while ( true )
{
cin >> a; cin >> b;
//process

if ( /*chk_condition */ )
break;
}

what should be the chk_condition over here

thx in anticipation.. :)
Post Reply

Return to “Volume 1 (100-199)”