Created
October 8, 2015 02:31
-
-
Save kunishi/947c4c38018214e826f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @JUDGE_ID: 26089BN 100 C "" */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define MAX 1000000 | |
#define UNDEF -1 | |
int table[MAX+1]; | |
int collatz(int x) | |
{ | |
unsigned long n = x; | |
int count = 0; | |
if (table[x] != UNDEF) { | |
return table[x]; | |
} | |
while (n != 1) { | |
if (n % 2 == 0) { | |
n = n / 2; | |
} else { | |
n = 3 * n + 1; | |
} | |
count ++; | |
if (n <= MAX && table[n] != UNDEF) { | |
table[x] = table[n] + count; | |
return table[x]; | |
} | |
} | |
return count; | |
} | |
int main() | |
{ | |
int max; | |
int result; | |
int n; | |
int i; | |
int from, to, temp; | |
for (i = 1; i < MAX+1; i ++) { | |
table[i] = UNDEF; | |
} | |
table[1] = 1; | |
while (scanf("%d %d", &from, &to) == 2) { | |
max = 0; | |
if (from < to) { | |
for (i = from; i <= to; i ++) { | |
if ((result = collatz(i)) > max) { | |
max = result; | |
} | |
} | |
} else { | |
for (i = to; i <= from; i ++) { | |
if ((result = collatz(i)) > max) { | |
max = result; | |
} | |
} | |
} | |
printf("%d %d %d\n", from, to, max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment