void main() { std::ifstream("siruri2.in"); std::ofstream("siruri2.out"); short requirement, ord; in >> requirement >> ord; long long n = requirement == 1 ? fibonacci(ord) : iccanobif(ord); int num_divisors = numOfDivisors(n); out << n << ' ' << num_divisors; } void main() { std::ifstream("siruri2.in"); std::ofstream("siruri2.out"); short requirement, ord; long long n = requirement == 1 ? fibonacci(ord) : iccanobif(ord); int num_divisors = numOfDivisors(n); out << n << ' ' << num_divisors; } void main() { std::ifstream("siruri2.in"); std::ofstream("siruri2.out"); short requirement, ord; in >> requirement >> ord; long long n = requirement == 1 ? fibonacci(ord) : iccanobif(ord); int num_divisors = numOfDivisors(n); out << n << ' ' << num_divisors; } Efficiency
numOfDivisors()
numOfDivisorsandexponentdo not need to belong. Usingintis sufficient, and should be slightly faster.nis along long, where asfactoris only along. Sofactorwill be repeated promoted to along longfor the operationsn % factorandn /= factor. You might find a speed improvement by actually declaringfactoras along longto avoid the repeated type promotion.factoris only along, sofactor * factoris also only along, and may overflow when loopingwhile (factor * factor <= n). Using along longwill avoid this, which may prevent the loop from running for a long time ifnis prime and larger than along.If
n % factor == 0, then the exponent counting inner loop is entered, and the first thing that is done isn % factor, which is already known to be zero. Using ado { ... } while (!(n % factor));loop will prevent the redundant calculation.The outer loop starts at
n=2, and has anifstatement to choose between incrementingnby2, or setting it to3. If2was handled as a special case, then the loop could unconditionally increment by2, eliminating theifstatement for another speed gain. To handle the2^exponentcase, simply count the number of trailing zeros in the binary representation ofn.Your factor finder is testing all odd numbers whose square is less than
n. You only need to testfactornumbers which are prime. Other than 2 & 3, all prime numbers can be generated from6k-1and6k+1. Or maybe use a prime sieve ... you are allowed 2MB of memory ...
iccanobif()
You are computing...
while (...) { a = inverted(a) + inverted(b); // #1 ... b = inverted(a) + inverted(b); // #2 } When you are executing statement #2, you've already computed inverted(b) during statement #1, above. If you cached that value, you wouldn't need to invert it a second time.
Similarly, when computing statement #1 in subsequent loops, you've already computed inverted(a) during statement #2, below, on the previous iteration. If you cached that value, you wouldn't need to invert it a second time.
General
Add vertical white space after #includes, after global variables, and between functions.
Add whitespace around operators. Ie, a += b; instead of a+=b;.
Don't use using namespace std;. Simply use:
std::ifstream in("..."); std::ofstream out("..."); numOfDivisors() should return the answer, not print the answer.
fibonacci() should return the Fibonacci value, not print the value and call another function which also has the side-effect of additional printing. Ditto for iccanobif().
main() is declare to return an int, but doesn't return anything.
If the above changes were made, then in and out don't need to be global variables; they could be made local to the main function:
void main() { std::ifstream("siruri2.in"); std::ofstream("siruri2.out"); short requirement, ord; long long n = requirement == 1 ? fibonacci(ord) : iccanobif(ord); int num_divisors = numOfDivisors(n); out << n << ' ' << num_divisors; }