求 (1! + 2! + 3! + 4! + ...... n!) % 494273703. n <= 1000000000000000000. (%是取模的意思)
时间: 2023-11-15 21:03:14 浏览: 37
要求 (1! + 2! + 3! + 4! + ...... n!) % 494273703,可以使用循环计算每个数的阶乘并累加,最后再对结果取模。但是由于n的范围非常大,直接计算阶乘会导致溢出,因此需要使用取模运算的性质,即(a * b) % c = ((a % c) * (b % c)) % c,来避免溢出。具体实现如下:
long long sum = 0;
long long fact = 1;
for (int i = 1; i <= n; i++) {
fact = (fact * i) % 494273703;
sum = (sum + fact) % 494273703;
}
printf("%lld\n", sum);
相关问题:
1. 如何计算大数的阶乘?
2. 取模运算有哪些性质?
相关问题
求 (1! + 2! + 3! + 4! + ...... n!) % 494273703. n <= 1000000000000000000. c语言
这道题目需要用到大数阶乘和取模的知识。由于n的范围非常大,我们需要使用高精度计算来计算阶乘和。同时,由于结果需要对一个很大的质数取模,我们需要使用快速幂算法来计算幂的模运算。
以下是C语言的代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#define MOD 494273703
typedef long long ll;
ll pow_mod(ll a, ll b, ll p) {
ll ans = 1 % p;
while (b) {
if (b & 1) ans = ans * a % p;
a = a * a % p;
b >>= 1;
}
return ans;
}
ll fac_mod(ll n, ll p) {
ll ans = 1 % p;
for (ll i = 1; i <= n; i++) {
ans = ans * i % p;
}
return ans;
}
ll fac_sum_mod(ll n, ll p) {
if (n == 0) return 0;
if (n == 1) return 1;
ll k = n / p;
ll ans = pow_mod(2, k, p) * fac_sum_mod(n / p, p) % p;
for (ll i = k * p + 1; i <= n; i++) {
ans = (ans + fac_mod(i, p)) % p;
}
return ans;
}
int main() {
ll n;
scanf("%lld", &n);
printf("%lld\n", fac_sum_mod(n, MOD));
return 0;
}
```
c语言求s=1!+2!+3!+...n!
以下是C语言求s=1!+2!+3!+...n!的代码:
```c
#include <stdio.h>
int main()
{
int n, i, j;
long long fact = 1, sum = 0;
printf("请输入一个正整数:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact = 1;
for (j = 1; j <= i; j++)
{
fact *= j;
}
sum += fact;
}
printf("1!+2!+3!+...+%d! = %lld\n", n, sum);
return 0;
}
```
运行结果:
```
请输入一个正整数:5
1!+2!+3!+...+5! = 153
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)