SPOJ Problem 24:Small factorials

求阶乘,注意数据范围,要用高精。

#include<cstdio>
#include<cstring>
int a[300];
int n,x,y,l,i,j;
int main(){
    for (scanf("%d",&n);n--;){
        scanf("%d",&x);
        memset(a,0,sizeof(a));
        a[1]=1;l=1;
        for (i=1;i<=x;i++){
            y=0;
            for (j=1;j<=l;j++){
                a[j]=a[j]*i+a[j-1]/10;
                a[j-1]%=10;
            }
            while(a[l]>9){a[++l]=a[l-1]/10;a[l-1]%=10;}
        }
        for (i=l;i;i--)printf("%d",a[i]);
        printf("\n");
    }
}
时间: 2024-12-28 14:35:45

SPOJ Problem 24:Small factorials的相关文章

SPOJ Python Day2: Small factorials

24. Small factorials 这题目非常简单,求"小整数(1-100)"的阶乘.题目规定了时间和程序大小. 所以能想到的最简单的循环,递归,和全算出来查表都是不行的. 正确的方法的算法,如这个博客所示,写的非常清楚了,数组进位法: http://www.open-open.com/home/space-135360-do-blog-id-9620.html 作者的例子举的也非常清晰. 但是...神奇的python有reduce函数,我也惊讶这个函数算阶乘这么快,直接可以AC

素数筛法--SPOJ Problem 2 Prime Generator

质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单,看它是否能被2到sqrt(N)之间的整数整除即可. def isPrime(n): if n%2==0: return False for i in xrange(3,int(math.sqrt(n)+1),2): if n%i==0: return False return True 不过要找出1

欧拉计划(python) problem 24

Lexicographic permutations Problem 24 A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexic

SPOJ Problem 7974:What&#39;s Next

求数列下一项,啥都不说了,贡献了N多WA... #include<cstdio> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)&&(a||b||c)){ if (b*2==c+a&&b!=a) printf("AP %d\n",2*c-b); else printf("GP %d\n",c*c/b); } return

SPOJ Problem 2: Prime Generator

嗯..在SPOJ上刷的第二题. 一开始不知道哪错了,后来发现i出现了两遍.. 因为m<10^9,所以用素数筛筛32000以内的数,开一个4000的数组存储就行.然后再从n开始用素数筛,总之效率还行. 代码如下: //0.01s 3.2M #include<cstdio> #include<cstring> #include<cmath> int n,i,j,t,m,k,tot; int a[100005],b[4000]; int prime[40000]; in

SPOJ Problem 9948:Will it ever stop

如题..http://www.spoj.com/problems/WILLITST/ #include<cstdio> long long n; int main(){ scanf("%lld",&n); while(n>1){ if (n==3||n==6){printf("NIE\n");return 0;} if (n&1)n=(n+1)/2*3;else n>>=1; } printf("TAK\n&q

SPOJ Problem 302:Count on Cantor

题目不复述了,自己看吧.http://www.spoj.com/problems/CANTON/一如既往的暴力.. #include<cstdio> int n,i,j,s,ans; int main(){ scanf("%d",&n); while(n--){ scanf("%d",&s); printf("TERM %d IS ",s); for (i=1;s>i;i++){ s-=i; } if (i&am

Project Euler:Problem 34 Digit factorials

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. #include <iostream> #include <v

SPOJ Problem:A Game with Numbers

突然就看到SPOJ升级了,让我好不适应.. 这一一道博弈论的题目,可以先前面暴力一下,易得小于十的为必胜态,十的时候必败,然后11到19又必胜,而且发现只要各位为零且这个数不为零就必败. 再依次验证上百,上千的数.. #include<cstdio> #include<cstring> int n; int main(){ scanf("%d",&n); if (n%10==0&&n!=0)printf("2\n");