Guest User

Untitled

a guest
Nov 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. /* Pegadinha: a questao avisa que nao vai estourar um int de 32 bits, mas nao fala "signed integer".
  4. * (o int normal soh suporta ateh 31 bits sem overflow)
  5. * Apos este detalhe, eh basicamente a simulacao mesmo, mas podemos guardar o resultado de calculos
  6. * anteriores para evitar retrabalho jah que os numeros se repetem muito.
  7. */
  8.  
  9. using namespace std;
  10.  
  11. int a, b, c, d, temp[1000001];
  12.  
  13. int calc(unsigned int n){
  14. int retorno;
  15. if(n < 1000001 && temp[n]) return temp[n];
  16. if(n&1)
  17. retorno = calc(3*n +1);
  18. else
  19. retorno = calc(n >> 1);
  20. if(n < 1000001) return temp[n] = retorno+1;
  21. return retorno+1;
  22. }
  23.  
  24. int main(){
  25. temp[1] = 1;
  26. while(scanf("%d %d", &a, &b) == 2){
  27. int maior = 0;
  28. if(a > b){
  29. c = b;
  30. d = a;
  31. }
  32. else{
  33. c = a;
  34. d = b;
  35. }
  36. for(int i = c; i <= d; ++i) if(calc(i) > maior) maior = temp[i];
  37. printf("%d %d %d\n", a, b, maior);
  38. }
  39. return 0;
  40. }
Add Comment
Please, Sign In to add comment