UVa 324 Factorial Frequencies (高精度)

本文介绍了一种计算大数阶乘中各数字出现频率的方法,并提供了一个完整的C++实现示例。该方法适用于计算高达366!的阶乘,能够帮助预测未来趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

324 - Factorial Frequencies

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=260

In an attempt to bolster her sagging palm-reading business, Madam Phoenix has decided to offer several numerological treats to her customers. She has been able to convince them that the frequency of occurrence of the digits in the decimal representation of factorials bear witness to their futures. Unlike palm-reading, however, she can't just conjure up these frequencies, so she has employed you to determine these values.

Recall that the definition of n! (that is, n factorial) is just tex2html_wrap_inline28 . As she expects to use either the day of the week, the day of the month, or the day of the year as the value of n, you must be able to determine the number of occurrences of each decimal digit in numbers as large as 366 factorial (366!), which has 781 digits.

Input and Output

The input data for the program is simply a list of integers for which the digit counts are desired. All of these input values will be less than or equal to 366 and greater than 0, except for the last integer, which will be zero. Don't bother to process this zero value; just stop your program at that point. The output format isn't too critical, but you should make your program produce results that look similar to those shown below.

Madam Phoenix will be forever (or longer) in your debt; she might even give you a trip if you do your job well!

Sample Input

3
8
100
0

Sample Output

3! --
   (0)    0    (1)    0    (2)    0    (3)    0    (4)    0
   (5)    0    (6)    1    (7)    0    (8)    0    (9)    0
8! --
   (0)    2    (1)    0    (2)    1    (3)    1    (4)    1
   (5)    0    (6)    0    (7)    0    (8)    0    (9)    0
100! --
   (0)   30    (1)   15    (2)   19    (3)   10    (4)   10
   (5)   14    (6)   19    (7)    7    (8)   14    (9)   20

完整代码:

/*0.022s*/

#include<bits/stdc++.h>
using namespace std;
const int maxn = 800;

char outputstr[maxn];///此字符串为bign中s的拷贝,用于输出
int dig[10];

struct bign
{
	int len, s[maxn];

	bign()
	{
		memset(s, 0, sizeof(s));
		len = 1;
	}

	bign(int num)
	{
		*this = num;
	}

	bign(const char* num)
	{
		*this = num;
	}

	bign operator = (const int num)
	{
		char s[maxn];
		sprintf(s, "%d", num);
		*this = s;
		return *this;
	}

	bign operator = (const char* num)
	{
		len = strlen(num);
		for (int i = 0; i < len; i++) s[i] = num[len - i - 1] & 15;
		return *this;
	}

	///输出
	const char* str() const
	{
		if (len)
		{
			for (int i = 0; i < len; i++)
				outputstr[i] = '0' + s[len - i - 1];
			outputstr[len] = '\0';
		}
		else strcpy(outputstr, "0");
		return outputstr;
	}

	///去前导零
	void clean()
	{
		while (len > 1 && !s[len - 1]) len--;
	}

	///加
	bign operator + (const bign& b) const
	{
		bign c;
		c.len = 0;
		for (int i = 0, g = 0; g || i < max(len, b.len); i++)
		{
			int x = g;
			if (i < len) x += s[i];
			if (i < b.len) x += b.s[i];
			c.s[c.len++] = x % 10;
			g = x / 10;
		}
		return c;
	}

	///减
	bign operator - (const bign& b) const
	{
		bign c;
		c.len = 0;
		for (int i = 0, g = 0; i < len; i++)
		{
			int x = s[i] - g;
			if (i < b.len) x -= b.s[i];
			if (x >= 0) g = 0;
			else
			{
				g = 1;
				x += 10;
			}
			c.s[c.len++] = x;
		}
		c.clean();
		return c;
	}

	///乘
	bign operator * (const bign& b) const
	{
		bign c;
		c.len = len + b.len;
		for (int i = 0; i < len; i++)
			for (int j = 0; j < b.len; j++)
				c.s[i + j] += s[i] * b.s[j];
		for (int i = 0; i < c.len - 1; i++)
		{
			c.s[i + 1] += c.s[i] / 10;
			c.s[i] %= 10;
		}
		c.clean();
		return c;
	}

	///除
	bign operator / (const bign &b) const
	{
		bign ret, cur = 0;
		ret.len = len;
		for (int i = len - 1; i >= 0; i--)
		{
			cur = cur * 10;
			cur.s[0] = s[i];
			while (cur >= b)
			{
				cur -= b;
				ret.s[i]++;
			}
		}
		ret.clean();
		return ret;
	}

	///模、余
	bign operator % (const bign &b) const
	{
		bign c = *this / b;
		return *this - c * b;
	}

	bool operator < (const bign& b) const
	{
		if (len != b.len) return len < b.len;
		for (int i = len - 1; i >= 0; i--)
			if (s[i] != b.s[i]) return s[i] < b.s[i];
		return false;
	}

	bool operator > (const bign& b) const
	{
		return b < *this;
	}

	bool operator <= (const bign& b) const
	{
		return !(b < *this);
	}

	bool operator >= (const bign &b) const
	{
		return !(*this < b);
	}

	bool operator == (const bign& b) const
	{
		return !(b < *this) && !(*this < b);
	}

	bool operator != (const bign &a) const
	{
		return *this > a || *this < a;
	}

	bign operator += (const bign &a)
	{
		*this = *this + a;
		return *this;
	}

	bign operator -= (const bign &a)
	{
		*this = *this - a;
		return *this;
	}

	bign operator *= (const bign &a)
	{
		*this = *this * a;
		return *this;
	}

	bign operator /= (const bign &a)
	{
		*this = *this / a;
		return *this;
	}

	bign operator %= (const bign &a)
	{
		*this = *this % a;
		return *this;
	}
} a[367];

int main()
{
	int i, n, len;
	a[1] = 1;
	for (i = 2; i < 367; ++i)
		a[i] = a[i - 1] * i;
	while (scanf("%d", &n), n)
	{
		memset(dig, 0, sizeof(dig));
		len = strlen(a[n].str());
		for (i = 0; i < len; ++i)
			++dig[outputstr[i] & 15];
		printf("%d! --\n", n);
		for (i = 0; i < 10; ++i)
		{
			printf("   (%d)%5d", i, dig[i]);
			if (i == 4) putchar(10);
		}
		putchar(10);
	}
	return 0;
}

### 高精度计算在C++中的实现 在C++中,高精度计算通常涉及处理超出内置整数类型的范围的数据。由于标准库并未提供直接支持大数运算的功能,因此开发者需自行设计解决方案或借助第三方库。 #### 使用自定义类实现高精度计算 一种常见的做法是创建一个封装大数操作的类。该类可以基于`std::vector<int>`存储每一位数字,并重载算术运算符以支持加减乘除等基本操作[^3]。例如: ```cpp class BigInteger { private: std::vector<int> digits; public: // 构造函数和其他成员函数省略... friend BigInteger operator+(const BigInteger& a, const BigInteger& b); friend BigInteger operator-(const BigInteger& a, const BigInteger& b); friend BigInteger operator*(const BigInteger& a, const BigInteger& b); friend std::pair<BigInteger, BigInteger> divide(const BigInteger& dividend, const BigInteger& divisor); // 返回商和余数 }; ``` 上述代码片段展示了如何构建一个基础的大数类框架。对于具体的实现细节,可参考提供的分治算法示例[^2],其中包含了长除法的核心逻辑。 #### 借助第三方库简化开发过程 除了手动编写外,还可以利用成熟的开源项目来完成这项任务。以下是几个推荐的选择: - **GMP (GNU Multiple Precision Arithmetic Library)** GMP 是一款广泛使用的高性能多精度数学库,适用于各种平台上的任意大小整数、有理数以及浮点数的操作[^1]。 - **Boost.Multiprecision** Boost 库的一部分,提供了易于集成且灵活的接口用于执行精确数值计算[^4]。 如果追求效率与稳定性,则优先考虑采用经过充分测试的专业工具而非重新发明轮子;但如果目标在于深入理解底层原理或者锻炼编程技巧的话,那么亲手打造专属版本无疑更具教育意义。 ```cpp #include <boost/multiprecision/cpp_int.hpp> namespace mp = boost::multiprecision; mp::cpp_int factorial(mp::cpp_int n) { return (n <= 1) ? 1 : n * factorial(n - 1); } int main(){ mp::cpp_int result = factorial(100); std::cout << "Factorial of 100 is: " << result << "\n"; } ``` 此段示范了调用 `Boost.Multiprecision` 来求解阶乘问题的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值