Fibonacci, a famous Italian mathematician in the Mediaeval Era, had discovered a series of natural numbers with multiple applications, a string that bears his name:
Fibonacci (n) = 1, if n = 1 or n = 2 Fibonacci (n) = Fibonacci (n−1) + Fibonacci (n−2), if n>2
Fascinated by Fibonacci's line, and especially the applications of this string in nature, Iccanobif, a mathematician in the making, created a string and he named him:
Iccanobif (n) = 1, if n = 1 or n = 2 Iccanobif (n) = reversed (Iccanobif (n-1)) + reversed (Iccanobif (n-2)), if n > 2
Obtaining the following rows:
Fibonacci: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Iccanobif: 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, 836, ...
Iccanobif now wonders which number has more natural divisors: the n-th term in the Fibonacci row or the n-th term in the Iccanobif row. Requirements:
Write a program that reads n natural number and displays:
a) the n-th term in Fibonacci's row and its number of divisors
b) the n-th term of the Iccanobif string and its number of divisors
Input data
The input file siruri2.in contains on the first line a natural number p. For all input tests, the p number can only have a value of 1 or value 2. A natural number n is found on the second line of the file.
Output data
If the value of p is 1, only a) of the requirements will be solved. In this case, in the output file siruri2.out, the n-th term in Fibonacci string and its number of divisors will be written.
If the value of p is 2, only the point b) of the requirements will be solved. In this case, in the output file siruri2.out the n-th term in Iccanobif string and its number of divisors will be written Restrictions and clarifications
1 ≤ n ≤ 50 For the correct resolution of the first requirement, 50% of the score is awarded, and 50% of the score is awarded for the second requirement.
Limits: - time: 1 second; - memory: 2 MB/ 2 MB.
Example 1
siruri2.in
1 8
siruri2.out
21 4
Example 2
siruri2.in
2 9
siruri2.out
124 6
Explanations
For the first example: The eighth term in Fibonacci's string is 21 and 21 has 4 divisors. (p being 1 solves only requirement a)
For the second example: The ninth term in Iccanobif's string is 124, and 124 has six divisors. (p being 2 solves only requirement b)
Here is my code, which doesn't execute all given tests in time (exceeds time limit for 3 tests):
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("siruri2.in");
ofstream out("siruri2.out");
void numOfDivisors (long long n)
{
long numOfDivisors = 1, factor = 2;
while (factor * factor <= n)
{
if (!(n % factor))
{
long exponent = 0;
while (!(n % factor))
{
n /= factor;
exponent ++;
}
numOfDivisors *= exponent + 1;
}
if (factor == 2)
factor = 3;
else factor += 2;
}
if (n > 1)
{
numOfDivisors *= 2;
}
out << numOfDivisors;
}
long long inverted (long long a)
{
long long b=0;
while (a)
{
b = b * 10 + a % 10;
a /= 10;
}
return b;
}
void fibonacci (short ord)
{
long long a = 1, b = 1;
if (ord < 3)
{out << "1 1";}
else
{
ord -= 2;
while (ord)
{
a+=b;
ord--;
if (!ord)
{
out << a << " ";
numOfDivisors(a);
break;
}
else
{
b+=a;
ord--;
if (!ord)
{
out << b << " ";
numOfDivisors(b);
}
}
}
}
}
void iccanobif (short ord)
{
long long a = 1, b = 1;
if (ord < 3)
out << "1 1";
else
{
ord -= 2;
while (ord)
{
a = inverted(a) + inverted(b);
ord--;
if (!ord)
{
out << a << " ";
numOfDivisors(a);
break;
}
else
{
b = inverted(a) + inverted(b);
ord--;
if (!ord)
{
out << b << " ";
numOfDivisors(b);
}
}
}
}
}
int main()
{
short requirement, ord;
in >> requirement >> ord;
if (requirement == 1)
fibonacci(ord);
else
iccanobif(ord);
}