社区讨论

995!样例都不对……自然是0分……大佬们帮帮

P1009[NOIP 1998 普及组] 阶乘之和参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mli0y9vq
此快照首次捕获于
2026/02/11 20:47
上周
此快照最后确认于
2026/02/13 20:30
6 天前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
string add(string s1, string s2) {
	string ans;
	int l = s1.size() - 1, r = s2.size() - 1, carry = 0;
	while (l >= 0 || r >= 0) {
		int a = (l >= 0 ? s1[l--] - '0' : 0);
		int b = (r >= 0 ? s2[r--] - '0' : 0);
		carry += a + b;
		ans += carry % 10 + '0';
		carry /= 10;
	}
	if (carry) ans += "1";
	reverse(ans.begin(), ans.end());
	return ans;
}
string mul(string a, string b) {
	if (a == "0" || b == "0") return "0";
	int l1 = a.size(), l2 = b.size(), x = 0;
	string ans(l1 + l2 - 1, '0');
	for (int i = l1 - 1; i >= 0; i--) {
		x = 0;
		for (int j = l2 - 1; j >= 0; j--) {
			int c1 = a[i] - '0', c2 = b[j] - '0';
			x += c1 * c2 + ans[i + j] - '0';
			ans[i + j] = x % 10 + '0';
			x /= 10;
		}
		if (i) ans[i - 1] += x;
	}
	if (x) ans = to_string(x) + ans;
	return ans;
}
int n;
string ans = "1";
int main() {
	cin >> n;
	for (int i = 1; i < n; i++) ans = add(ans, mul(ans, to_string(i)));
	cout << ans << endl;
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...