hoj 2934 Simple Math Question

As we know, n! is defined as n*(n-1)*(n-2)…2*1

Now, bigcaicai has a simple question: when giving you two integers: A and B (A ≤ B), can you give the sum of i!(A ≤ i ≤ B)?

Input

The input contains mutiple cases. Each case consists of two integers: A and B(0 < A ≤ B < 21).

The input file will end with the case 0 0. This case should not be processed.

Output

Output a line containing the sum of i!(A ≤ i ≤ B).

Sample Input

1 4
2 5
0 0

Sample Output

33
152

题意:A到B的阶乘的和。简单的用数组模拟大数加法。

代码如下:

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 30
int buf[N][N] = { 0 };
int ans[N + N];
void Init()
{
	int i, j, k;
	for (i = 0; i < N; i++)
		buf[i][0] = 1;
	for (i = 0; i < N; i++)
	{
		for (j = 1; j <= i + 1; j++)
		{
			int c = 0;
			for (k = 0; k < N; k++)
			{
				int temp = buf[i][k] * j + c;
				buf[i][k] = temp % 10;
				c = temp / 10;
			}
		}
	}
}
void solve(int m, int n)
{
	memset(ans, 0, sizeof(ans));
	int i, j;
	for (i = m - 1; i < n; i++)
	{
		int c = 0;
		for (j = 0; j < N; j++)
		{
			ans[j] += buf[i][j] + c;
			c = ans[j] / 10;
			ans[j] %= 10;
		}
	}
	j = N - 1;
	while (!ans[j])
		j--;
	for (; j >= 0; j--)
		printf("%d", ans[j]);
	printf("\n");
}

int main()
{
	Init();
	int m, n;
	while (EOF != scanf("%d %d", &m, &n) && 0 != m && 0 != n)
		solve(m, n);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值