Skip to main content
Forgot to read in requirement & ord
Source Link
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103
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; } 
Source Link
AJNeufeld
  • 35.3k
  • 5
  • 41
  • 103

Efficiency

numOfDivisors()

  • numOfDivisors and exponent do not need to be long. Using int is sufficient, and should be slightly faster.

  • n is a long long, where as factor is only a long. So factor will be repeated promoted to a long long for the operations n % factor and n /= factor. You might find a speed improvement by actually declaring factor as a long long to avoid the repeated type promotion.

  • factor is only a long, so factor * factor is also only a long, and may overflow when looping while (factor * factor <= n). Using a long long will avoid this, which may prevent the loop from running for a long time if n is prime and larger than a long.

  • If n % factor == 0, then the exponent counting inner loop is entered, and the first thing that is done is n % factor, which is already known to be zero. Using a do { ... } while (!(n % factor)); loop will prevent the redundant calculation.

  • The outer loop starts at n=2, and has an if statement to choose between incrementing n by 2, or setting it to 3. If 2 was handled as a special case, then the loop could unconditionally increment by 2, eliminating the if statement for another speed gain. To handle the 2^exponent case, simply count the number of trailing zeros in the binary representation of n.

  • Your factor finder is testing all odd numbers whose square is less than n. You only need to test factor numbers which are prime. Other than 2 & 3, all prime numbers can be generated from 6k-1 and 6k+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; }