mf wrote:Looks like for n=113383, the integers generated in 3n+1 sequence will overflow 32-bit integers, and this somehow leads into an infinite periodic loop.
The problem statement says "no operation overflows a 32-bit integer", so you can assume that in judge's input there will be no test cases triggering such overflows, and submit your program as is.
Hmm, now it's telling me WA. I thought maybe it was because I actually just output the input string, instead of the integers themselves, so I changed it to output the integers--and made sure they came out in the same order as the input--but it's still giving me WA. I wonder if that has anything to do with the whole multiple line input scheme I have working in it (where you just press enter twice to get the output). I noticed some people in here just use one-line-at-a-time output in an infinite loop, when the description mentions it has to be capable of a series of integers, so I don't know.
Edit: Okay, so I changed it to one-line-at-a-time output, but now it's telling me Time Limit Exceeded. Testing on my own machine, it can do 1 to 1000000 in 1 second. Are they using some kind of ancient Pentium 1 or something? What the hell is going on?
Here's my latest code:
Code: Select all
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int nCalc (int i, int j)
{
unsigned long int n, a = 0;
do
{
unsigned long int di = i;
for (n = 1; di != 1; n++)
{
if (n == 1)
{
continue;
}
if ((di % 2) != 0)
{
di = (3 * di) + 1;
continue;
}
else
{
di = di/2;
}
if (n > a)
a = n;
}
if (i == 1)
n = 1;
i++;
}while(i <= j);
return a;
}
int main()
{
int n, a, b;
vector<int> cycle, i, j, s;
cycle.clear();
for (int counter = 0; ; counter++)
{
cin >> a >> b;
if (a > b)
{
int temp = a;
a = b;
b = temp;
s.push_back(1);
}
else s.push_back(0);
i.push_back(a);
j.push_back(b);
n = nCalc(i[counter], j[counter]);
if (s[counter] == 1)
{
int temp = i[counter];
i[counter] = j[counter];
j[counter] = temp;
}
cout << i[counter] << " " << j[counter] << " " << n << "\n";
}
return 0;
}
Edit 2: It seems if I have it in a for loop with no exit argument, it tells me Time Limit Exceeded, and if I have it in an infinite while loop, it tells me Wrong Answer. I have come to the conclusion that the Online Judge just hates me.