-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.cpp
More file actions
80 lines (54 loc) · 1.8 KB
/
Copy path11.cpp
File metadata and controls
80 lines (54 loc) · 1.8 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
int main() {
// some formulas
// 1. (a+b) % M = ((a % M) + (b % M)) % M
// 2. (a*b) % M = ((a % M) * (b % M)) % M
// 3. (a-b) % M = ((a % M) - (b % M) + M) % M
// 4. (a/b) % M = ((a % M) * ((b^-1) % M)) % M
// b^-1 is called multiplicative inverse
// Given a number N. Print its factorial.
// Constraints
// 1 <= N <= 100
// print answer module M
// where M = 47
// modulo is asked when we know we can calculate a large value but can't store it then question ask to return the modulo
// this will return value less than 47
// int n;
// cin>>n;
// long long fact = 1;
// for (int i = 2; i <= n; i++)
// {
// fact *= i;
// }
// cout<<fact<<endl;
// NOw here we will solve the problem
int n;
cin>>n;
int M = 47;
long long fact = 1;
for (int i = 2; i <= n; i++)
{
fact = (fact * i) % M;
// fact = (fact + i) % M; // in case of addition we can do like this also
}
cout<<fact<<endl;
// let's say we need to calculate 5! = 1 * 2 * 3 * 4 * 5
// (1 * 2 * 3 * 4 * 5) % 47
// ((1 * 2 * 3 * 4) % 47 * (5 % 47)) % 47
// ((1 * 2 * 3 * 4) % 47 * 5 ) % 47
// ((((1 * 2 * 3) % 47 * (4 % 47))) % 47 * 5) % 47
// .
// .
// .
// ((1 % 47) * (2 % 47) * (3 % 47) * (4 % 47) * (5 % 47)) % 47
// similarly we can do this for addition
// ((1 % 47) + (2 % 47) + (3 % 47) + (4 % 47) + (5 % 47)) % 47
// Mostly M = 10^9 + 7
// there are two significance of this value of M
// 1. this is very close to integer maximum, thus we can store ans in int
// 2. Multiplicative inverse (MMI),
// 10^9 + 7 is a prime number, and for M is prime, I can find mmi of every number from 1 to M
// this can help us in finding MMI which will be used in (a/b)%M
return 0;
}