#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; ll pwr(ll a, ll k, ll p){ if (k == 0) return 1; return (k % 2 == 0 ? pwr((a * a) % p, k / 2, p) : (a * pwr(a, k - 1, p)) % p); } //2 - 12 [11 entries] const int MAX_N = 12; const int MAXL = 4; ll ans [MAX_N - 1][MAXL] = { {2, 0, 0, 0}, {4, 2, 0, 0}, {8, 16, 0, 0}, {16, 100, 4, 0}, {32, 616, 72, 0}, {64, 4024, 952, 0}, {128, 28512, 11680, 0}, {256, 219664, 142800, 160}, {512, 1831712, 1788896, 7680}, {1024, 16429152, 23252832, 233792}, {2048, 157552000, 315549312, 5898240} }; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; ll p; cin >> n >> k >> p; if (n == 1){ cout << (k == 0 ? 1 : 0) << endl; return 0; } if (n <= MAX_N){ cout << (k > MAXL || k == 0 ? 0 : ans[n - 2][k - 1] % p) << endl; return 0; } if (k == 1){ cout << pwr(2, n - 1, p) << endl; return 0; } cout << 0 << endl; }
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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FOROP(i,a,b,op) for(int i=a;i<b;op) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vii vector<vi> #define pii pair<int, int> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; ll pwr(ll a, ll k, ll p){ if (k == 0) return 1; return (k % 2 == 0 ? pwr((a * a) % p, k / 2, p) : (a * pwr(a, k - 1, p)) % p); } //2 - 12 [11 entries] const int MAX_N = 12; const int MAXL = 4; ll ans [MAX_N - 1][MAXL] = { {2, 0, 0, 0}, {4, 2, 0, 0}, {8, 16, 0, 0}, {16, 100, 4, 0}, {32, 616, 72, 0}, {64, 4024, 952, 0}, {128, 28512, 11680, 0}, {256, 219664, 142800, 160}, {512, 1831712, 1788896, 7680}, {1024, 16429152, 23252832, 233792}, {2048, 157552000, 315549312, 5898240} }; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; ll p; cin >> n >> k >> p; if (n == 1){ cout << (k == 0 ? 1 : 0) << endl; return 0; } if (n <= MAX_N){ cout << (k > MAXL || k == 0 ? 0 : ans[n - 2][k - 1] % p) << endl; return 0; } if (k == 1){ cout << pwr(2, n - 1, p) << endl; return 0; } cout << 0 << endl; } |