-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ15655.cc
39 lines (34 loc) · 831 Bytes
/
SPOJ15655.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// SPOJ 15655: Product of factorials
// http://www.spoj.com/problems/FACTMUL/
//
// Solution: trick question (M is non-prime), ad-hoc
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
using namespace std;
#define ALL(c) c.begin(), c.end()
#define FOR(i,c) for(typeof(c.begin())i=c.begin();i!=c.end();++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define fst first
#define snd second
typedef long long LL;
const LL M = 109546051211ll;
// 109546051211 = 186583 * 587117
LL mulMod(LL a, LL b, LL M) {
LL r = a*b - LL(double(a)*b/M + 0.5)*M;
while (r < 0) r += M;
return r;
}
int main() {
LL f = 1, p = 1;
int n; cin >> n;
for (int i = 1; p && i <= n; ++i) {
f = (f * i) % M;
p = mulMod(p, f, M);
}
cout << p << endl;
}