Posted: Sat Dec 04, 2004 8:53 pm
Hi !! SePulTribe nice to know that you get AC !! and I thinking in put the input from 0...1000 in somewhere and the ouput,of course,
Keep posting !!
Keep posting !!
Code: Select all
#include <stdio.h>
#include <string.h>
char str1[1000];
char str2[4000];
char final[4000];
char cal[1500][4000];
int i, j, ii, len1, len2, tmp, limit, result, N;
int main(){
final[0] = 1;
cal[0][0] = 1;
len2 = 1;
for (ii=1;ii<=1000;ii++){
len1 = sprintf(str1,"%d",ii);
for (i=0;i<len2;i++){
str2[i] = final[i];
}
for (i=0;i<(len1+1)/2;i++){
tmp = str1[i] - '0';
str1[i] = str1[len1-i-1] - '0';
str1[len1-i-1] = tmp;
}
for (i=0;i<len1;i++){
for (j=0;j<len2;j++){
tmp = str2[j] * str1[i];
final[i+j] += tmp;
final[i+j+1] += final[i+j]/10;
final[i+j] = final[i+j] % 10;
}
}
for (i=4000;final[i]==0;i--){
if (i==0){
break;
}
}
len2 = i+1;
for (i=0;i<len2;i++){
cal[ii][i] = final[i];
}
}
while (scanf("%d",&N)!=EOF){
printf("%d!\n",N);
for (i=4000;cal[N][i]==0;i--){
if (i==0){
break;
}
}
for (;i>=0;i--){
printf("%c",cal[N][i]+'0');
}
printf("\n");
}
return 0;
}
Code: Select all
char final[4000];
...
for (i=4000;final[i]==0;i--){
Code: Select all
for (i=4000;cal[N][i]==0;i--){
Code: Select all
#include <cstdio>
using namespace std;
#define BASE 1000000000
#define MAXNUM 999999999
struct bignum
{
long val[300];
long len;
};
bignum tab[1001];
void mul(bignum &a,long b,bignum &r)
{
long long tmp;
long i;
long carry=0;
for(i=0;i<a.len;i++)
{
tmp=long long(a.val[i])*b+carry;
r.val[i]=tmp%BASE;
carry=tmp/BASE;
}
if(carry!=0)
{
r.val[i++]=carry;
}
r.len=i;
}
void printNum(bignum &n)
{
long i=n.len-1;
printf("%d",n.val[i]);
for(--i;i>=0;i--)
printf("%09d",n.val[i]);
}
void main()
{
tab[0].val[0]=1;
tab[0].len=1;
long i;
for(i=1;i<=1000;i++)
mul(tab[i-1],i,tab[i]);
long n;
while(scanf("%d",&n)==1)
{
printf("%d!\n",n);
printNum(tab[n]);
printf("\n");
}
}
Code: Select all
#define MAX 3000
#define MAX_FACT 500
#define MAX_LEN 1140
void main()
{
char num1[MAX], num2[MAX], res[MAX];
char resultados[MAX_FACT][MAX_LEN];
int len1, len2, n, c;
/* Precalcalculate factorials to avoid TLE */
strcpy(resultados[0],"1");
for(c = 1; c < 500; c++)
{
sprintf(num1,"%d",c+1);
strcpy(num2,resultados[c-1]);
len1 = strlen(num1);
len2 = strlen(num2);
if(len1 < len2)
multiplicacion(num1,num2,res);
else
multiplicacion(num2,num1,res);
strcpy(resultados[c],res);
}
while(cin >> n)
cout << n << "!\n" << resultados[n-1] << endl;
}
from the problem description at http://online-judge.uva.es/p/v6/623.html.Assumptions: Value of a number ``n" which factorial should be calculated of does not exceed 1000 (although 500! is the name of the problem, 500! is a small limit).
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#define LEN 2600
#define MAX 1001
int main()
{
int num[MAX][LEN] , n , i ;
for( i = 0 ; i < MAX ; i++)
for( int j = 0 ; j < LEN ; j++ )
num[i][j] = 0 ;
num[0][0] = 1 ;
num[1][0] = 1 ;
for( i = 2 ; i < MAX ; i++ )
{
for( int j = 0 ; j < LEN - 1 ; j++ )
{
num[i][j] += num[i-1][j] * i ;
num[i][j+1] += num[i][j] / 10 ;
num[i][j] %= 10 ;
}
}
while( scanf("%d" , &n ) != EOF && n >= 0 )
{
printf("%d!\n" , n ) ;
for( i = LEN - 1 ; i >= 0 ; i-- )
if( num[n][i] != 0 ) break ;
for( ; i >= 0 ; i-- )
printf("%d" , num[n][i] ) ;
printf("\n") ;
}
return 0;
}