File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ #include < iostream>
4+ using namespace std;
5+ using ll = long long ;
6+
7+ ll N , K , P ;
8+
9+ ll power(ll n, ll m){
10+ if (m == 0 ) return 1 ;
11+ if (m == 1 ) return n;
12+ ll p = power(n,m>> 1 );
13+ if (m& 1 ) return p* p% P * n% P ;
14+ return p* p% P ;
15+ }
16+
17+ int main() {
18+ cin. tie(0 )- > sync_with_stdio(0 );
19+
20+ cin >> N >> K >> P ;
21+
22+ if (N == 2 ) return cout << (P == 2 ? 0 : 2 ), 0 ;
23+
24+ if (K >= 4 ) return cout << 0 , 0 ;
25+
26+ if (K == 2 ) {
27+ if (N >= 13 ) return cout << 0 , 0 ;
28+ ll fac = 1 ;
29+ for (int i = 1 ; i <= N ; i++ ) fac *= i;
30+ if (fac >= P ) return cout << 0 , 0 ;
31+ if (N <= 11 ){
32+ ll ans = 1 ;
33+ for (int i= 2 ;i<= fac;i++ ) ans = (ans * i) % P ;
34+ return cout<< ans,0 ;
35+ }
36+
37+ ll res = 1 ;
38+ for (int i= 479001601 ;i< P ;i++ ) res = (res * i) % P ;
39+
40+ cout<< (P - 1 ) * power(res,P - 2 ) % P ;
41+
42+ }
43+
44+ if (K == 3 ) {
45+ if (N >= 4 ) return cout << 0 , 0 ;
46+ ll ans = 1 ;
47+ for (int i = 1 ; i <= 720 ; i++ ) ans = (ans * i) % P ;
48+ cout << ans;
49+ }
50+
51+ }
52+
53+ ```
You can’t perform that action at this time.
0 commit comments