Reputation: 43
I try to run following code:
#include <stdio.h>
#include <stdlib.h>
int find_next(int act, unsigned long long survivors[], int n)
{
int i = act;
while (survivors[i] == 0)
{
i++;
i = i % n;
}
i = (i + 1) % n; // found first one, but need to skip
while (survivors[i] == 0)
{
i++;
i = i % n;
}// thats the guy
return i;
}
int main()
{
long long int lines;
long long int* results;
scanf_s("%llu", &lines);
results = new long long int[lines];
for (long long int i = 0; i < lines; i++) {
unsigned long long n, k;
scanf_s("%llu", &n);
scanf_s("%llu", &k);
unsigned long long* survivors;
survivors = new unsigned long long[n];
for (int m = 0; m < n; m++) {
survivors[m] = 1;
}
int* order;
order = new int[n];
int p = 0;
int act = 0;
while (p < n - 1)
{
act = find_next(act, survivors, n);
order[p] = act;
survivors[act] = 0; // dies;
p++;
}
order[p] = find_next(act, survivors, n);
if (k > 0)
{
results[i] = order[k - 1] + 1;
}
else
{
results[i] = order[n + k] + 1;
}
delete[] survivors;
delete[] order;
}
for (long long int i = 0; i < lines; i++) {
printf("%llu\n", results[i]);
}
delete[] results;
return 0;
}
My inputs are:
1
1111111111
-1
I am getting an exeption:
std::bad_array_new_length for large numbers
At line:
survivors = new unsigned long long[n];
How should I fix it that it wont show for such large numbers?
So far I tried all numeric types of n -> long long int, unsigned long and so on but everytime I was failing. Or maybe there is no way around that?
Upvotes: 0
Views: 445
Reputation: 238461
How should I fix it that it wont show for such large numbers?
P.S. Avoid owning bare pointers. In this case, I recommend using a RAII container such as std::vector
.
Upvotes: 1