Pagini recente » Istoria paginii runda/cnrv_2 | Cod sursa (job #686982) | Cod sursa (job #2422411) | Cod sursa (job #710483) | Cod sursa (job #1652013)
#include <iostream>
#include <fstream>
using namespace std;
long factorial(int n) {
if (n == 0 || n ==1)
return 1;
else return n * factorial(n-1);
}
long smallestNo(int p) {
if (p == 0)
return 1;
long output = 0;
long multipliesFound = 0;
while (multipliesFound < p) {
output++;
if (output%625 == 0) {
multipliesFound += 5;
long temp = output/625;
long multiplies = 0;
while (temp%5 == 0) {
multiplies++;
temp = temp/5;
}
multipliesFound += multiplies;
} else if (output%125==0) multipliesFound += 4;
else if (output%25==0) multipliesFound += 3;
else if (output%5==0) multipliesFound += 2;
else multipliesFound += 1;
}
output *= 5;
if (multipliesFound > p) output = -1;
return output;
}
long pow5(long exp) {
if (exp == 0) return 1;
else return 5 * pow5(exp-1);
}
long factSearch(long p) {
int k = 1;
int sum = 1;
int prevSum = 1;
while (sum < p) {
long aux = sum;
sum = sum*5 + 1;
prevSum = aux;
k++;
}
long output = 0;
if (sum == p) {
output = pow5(k);
} else output = pow5(k-1);
if (sum != p) {
long left = prevSum, right = sum;
long step = sum;
while (left != right && left < p && k > 0) {
step = (step-1)/5;
int bucket = (p-left)/step;
k--;
output += bucket * pow5(k);
left = left + bucket*step;
if (bucket == 5)
right = left + (bucket+1)*step+1;
else right = left + (bucket+1)*step;
//cout<<"step is: "<<step<<" bucket: "<<bucket<<" left: "<<left<<" right: "<<right<<endl;
}
if (left != right)
output = -1;
}
//cout<<"res is: "<<output<<endl;
return output;
}
int main(void) {
ifstream f("fact.in");
ofstream g("fact.out");
long p;
f>>p;
long output = factSearch(p);
g<<output;
return 0;
}