No both solution is will take either time limit exceed or a run time error
that is because multiplication & division operations are a time expensive so
you have to use shift register operations
that is my accepted code in java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
class The3n1Problem {
public static long []save=new long[600];
public long generate(long i){
long index=i;
long sum=1;
while(index>1){
if((index-1)>>1==(index)>>1){
index=(index*3)+1;
}
else{
index=index>>1;
}
if(index<600&&save[(int)index]!=0)return
save[(int)index]+sum;
sum++;
}
return sum;
}
public long max(long a,long b){
long max=0,s,g;
if(a>b){g=a;s=b;}
else{g=b;s=a;}
for(long i=s;i<=g;i++){
long res=generate(i);
if(i<600)save[(int)i]=res;
if(res>max)max=res;
}
return max;
}
public static void main (String args[])throws IOException {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
String s;
while((s=in.readLine())!=null){
StringTokenizer st =new StringTokenizer(s);
The3n1Problem z=new The3n1Problem();
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
System.out.println(a+" "+b+" "+z.max(a,b));
}
System.exit(0);
}
}
On 2 September 2011 23:48, addytheboss khandalkar <
[email protected]> wrote:
>
> hey hi,
>
> you can take the help of this code and modify it..
>
> Its made in Dev-C++ and thus not standard linux code.
>
> #include <stdio.h>
>
> int main()
> {
> int x , n , m, y, i ,max;
> int count;
> int arr[100000];
> max = 0;
>
> printf("press 0 to exit\n");
>
> while (1){
>
> scanf("%d%d" , &m, &n);
> i = m;
>
> if (m == 0){
> break;
> }
> while( i<=n ){
> count = 1;
>
> // printf("%d\n", i);
> y = i;
>
> while (y != 1 ){
> if ( y == 1 ){
> break;
> }
>
> if ( y& 1 ){
> y= 3*y+1;
> }else{
> y = y/2;
> }
> count++;
> }
>
>
>
> if (max < count){
> max = count;
> }
>
> i++;
>
> }
>
> printf("%d\n" , max);
> max = 0;
> }
> system("pause");
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.