Cod sursa(job #2561964)

Utilizator raizoSoare Antonio raizo Data 29 februarie 2020 11:22:48
Problema Factorial Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream in("fact.in");
ofstream out("fact.out");

//int power(int base, int exp);
bool factorial(int n,int p);
int multiply(int x,long long int res[],int res_size);

 int main() {
	int p, n;
    bool no_match=true;

	in >> p;

	//nrO = power(10, p);

	n=1;

	while (no_match){
    n++;
    no_match=factorial(n,p);

	}

    if (p == 0) { out << 1; }
	else if (n > 1) { out << n; }
	else out << -1;

	return 0;

}

 /*
 int power( int base, int exp) {
	 int result = 1;
	for (int c = 1;c <=exp;c++) {
		result = result * base;
	}
	return result;

}
*/

   int multiply(int x,int long long res[],int res_size){
        int carry=0;
        for(int i=0;i<res_size;i++){
            int prod=res[i]*x+carry;
            res[i]=prod%10;
            carry=prod/10;
        }

        while(carry){
            res[res_size]=carry%10;
            carry=carry/10;
            res_size++;
        }
    return res_size;
   }



    bool factorial(int n,int p) {
		long long int res[2000];
		res[0]=1;
		int res_size=1;

		for(int x=2;x<=n;x++){
            res_size=multiply(x,res,res_size);
		}
        for(int i=0;i<p;i++){
            if(res[i]!=0){return true;}
            }
        return false;
	}