Cod sursa(job #1126450)

Utilizator eraser_aCarp Andrei eraser_a Data 26 februarie 2014 23:25:42
Problema Factorial Scor 15
Compilator c Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <stdio.h>
#include <stdlib.h>

FILE *in, *out;

long long get(long long input)
{
	return ((((input<<2)+1)/5)*5)+5;
}

long long calculate_offset(int cifra, int offset);

long long get_baza5(long long input)
{
	int offset = 0;
	long long sum = 0;
	while(input)
	{
		int cifra = input%5;
//		printf("Cifra : %lld\n",cifra);
		sum = sum + calculate_offset(cifra,offset);
		input = input /5;		
		offset ++;
	}

	//printf("Rebuilt sum is : %lld\n",sum);
	return sum;
}

long long calculate_offset(int cifra, int offset)
{
	long long pow_5 = 1;
	if(offset == 0)
		return 0;

	while(offset > 0)
	{
		pow_5 = pow_5 * 5;
		offset --;
	}	
	return cifra*(pow_5-1)/4;

}

int main()
{
	long long N;
	long long result;
	in = fopen("fact.in","r");
	fscanf(in,"%lld",&N);
	fclose(in);

	//printf("N=%lld\n",N);	
	out = fopen("fact.out","w");
	result = get(N);
	//printf("result : %lld\n",result);
	if(N != get_baza5(result))
		result = -1;

	fprintf(out,"%lld",result);
	fclose(out);
	return 0;
}