I was solving a practice problem for a competition and I got this output in my test script. I was supposed to calculate this 37 digit number (84 bits) and I don't know how this would be possible in C++. Any ideas?
----------------------------------------------------------------------------------------------------
Exercise #8: Failure
Expected answer: Your answer:
2982946161059046714576278832998350000 18446744073709501616
----------------------------------------------------------------------------------------------------
Here is my code. Only the part where C
is 2
is executed. In my problem, where L
is the length of the given string, I need to calculate L! / <occuranceOfA> / <occuranceOfB> * ... * <occuranceOfZ>
(excluding the occurances that are equal to 0
)
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <string.h>
static std::string A[110000];
unsigned long long C;
unsigned long long factorial(unsigned long long n) {
if (n == 1) return 1;
else return n * factorial(n - 1);
}
void solve(std::string filename) {
std::ifstream fin(filename + ".in");
std::ofstream fout(filename + ".out");
std::cout << filename << std::endl;
unsigned long long c = 0;
fin >> C;
while (fin >> A[c]) c++;
if (C == 1) {
for (unsigned long long i=0; i<c-1; i++) {
std::next_permutation(A[i].begin(), A[i].end());
if (A[i] != A[i+1]) {
fout << A[i];
return;
}
}
} else {
unsigned long long result = factorial(A[0].length()), arr[26];
memset(arr, 0, 26*sizeof(*arr));
for (unsigned long long i=0; i<A[0].length(); i++) {
char c = A[0].at(i);
if ((int)c >= 97 && (int)c <= 122)
arr[(int)c - 97] += 1;
}
for (unsigned long long i=0; i<26; i++)
if (arr[i] != 0)
result /= factorial(arr[i]);
result -= c;
fout << result;
return;
}
}