#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int facN = 1e6 + 1;
const long long mod = 1e9 + 7;
long long fac[facN], invfac[facN];
long long power(int x, int y){
if (y==0) return 1;
long long v = power(x, y/2);
v *= v; v%= mod;
if (y&1) return v * x % mod;
else return v;
}
void Factorials(bool inverse = true){
fac[0] = invfac[0] = 1;
for (int i=1; i<facN; i++){
fac[i] = fac[i-1] * i % mod;
}
if (!inverse) return;
invfac[facN - 1] = power(fac[facN - 1], mod - 2);
for (int i=facN - 2; i>=0; i--){
invfac[i] = invfac[i+1] * (i + 1) % mod;
}
}
long long C(int n, int r){
return fac[n] * invfac[r] % mod * invfac[n-r] % mod;
}
long long P(int n, int r){
return fac[n] * invfac[n-r] % mod;
}
long long Solutions(int n, int r){
//x1 + .... + xn = r non negative integer solutions
return C(n + r - 1, n - 1);
}
long long inverse(int n){
return power(n, mod - 2);
}
//remember to update facN and mod to the value you want
//remember to actually call the Factorials function
void Solve()
{
Factorials();
int n, k; cin >> n >> k;
int ans = 0;
for (int i = 0; i <= k; i++){
int ways = C(n + 1, i) * power(k - i, n) % mod;
if (i & 1) ans -= ways;
else ans += ways;
}
ans %= mod;
if (ans < 0) ans += mod;
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
15956 KB |
Output is correct |
2 |
Correct |
20 ms |
15856 KB |
Output is correct |
3 |
Correct |
18 ms |
15956 KB |
Output is correct |
4 |
Correct |
19 ms |
15980 KB |
Output is correct |
5 |
Correct |
18 ms |
15872 KB |
Output is correct |
6 |
Correct |
19 ms |
15944 KB |
Output is correct |
7 |
Correct |
18 ms |
15904 KB |
Output is correct |
8 |
Correct |
18 ms |
15968 KB |
Output is correct |
9 |
Correct |
19 ms |
15956 KB |
Output is correct |
10 |
Correct |
19 ms |
15928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
15956 KB |
Output is correct |
2 |
Correct |
20 ms |
15856 KB |
Output is correct |
3 |
Correct |
18 ms |
15956 KB |
Output is correct |
4 |
Correct |
19 ms |
15980 KB |
Output is correct |
5 |
Correct |
18 ms |
15872 KB |
Output is correct |
6 |
Correct |
19 ms |
15944 KB |
Output is correct |
7 |
Correct |
18 ms |
15904 KB |
Output is correct |
8 |
Correct |
18 ms |
15968 KB |
Output is correct |
9 |
Correct |
19 ms |
15956 KB |
Output is correct |
10 |
Correct |
19 ms |
15928 KB |
Output is correct |
11 |
Correct |
20 ms |
16076 KB |
Output is correct |
12 |
Correct |
19 ms |
15880 KB |
Output is correct |
13 |
Correct |
18 ms |
15956 KB |
Output is correct |
14 |
Correct |
19 ms |
15956 KB |
Output is correct |
15 |
Correct |
19 ms |
15960 KB |
Output is correct |
16 |
Correct |
18 ms |
15888 KB |
Output is correct |
17 |
Correct |
19 ms |
15956 KB |
Output is correct |
18 |
Correct |
19 ms |
15980 KB |
Output is correct |
19 |
Correct |
19 ms |
15920 KB |
Output is correct |
20 |
Correct |
19 ms |
15928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
15956 KB |
Output is correct |
2 |
Correct |
20 ms |
15856 KB |
Output is correct |
3 |
Correct |
18 ms |
15956 KB |
Output is correct |
4 |
Correct |
19 ms |
15980 KB |
Output is correct |
5 |
Correct |
18 ms |
15872 KB |
Output is correct |
6 |
Correct |
19 ms |
15944 KB |
Output is correct |
7 |
Correct |
18 ms |
15904 KB |
Output is correct |
8 |
Correct |
18 ms |
15968 KB |
Output is correct |
9 |
Correct |
19 ms |
15956 KB |
Output is correct |
10 |
Correct |
19 ms |
15928 KB |
Output is correct |
11 |
Correct |
20 ms |
16076 KB |
Output is correct |
12 |
Correct |
19 ms |
15880 KB |
Output is correct |
13 |
Correct |
18 ms |
15956 KB |
Output is correct |
14 |
Correct |
19 ms |
15956 KB |
Output is correct |
15 |
Correct |
19 ms |
15960 KB |
Output is correct |
16 |
Correct |
18 ms |
15888 KB |
Output is correct |
17 |
Correct |
19 ms |
15956 KB |
Output is correct |
18 |
Correct |
19 ms |
15980 KB |
Output is correct |
19 |
Correct |
19 ms |
15920 KB |
Output is correct |
20 |
Correct |
19 ms |
15928 KB |
Output is correct |
21 |
Correct |
18 ms |
15960 KB |
Output is correct |
22 |
Correct |
18 ms |
15956 KB |
Output is correct |
23 |
Correct |
20 ms |
16052 KB |
Output is correct |
24 |
Correct |
19 ms |
15952 KB |
Output is correct |
25 |
Correct |
19 ms |
15960 KB |
Output is correct |
26 |
Correct |
20 ms |
15956 KB |
Output is correct |
27 |
Correct |
19 ms |
15940 KB |
Output is correct |
28 |
Correct |
20 ms |
15956 KB |
Output is correct |
29 |
Correct |
18 ms |
15972 KB |
Output is correct |
30 |
Correct |
19 ms |
15980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
15956 KB |
Output is correct |
2 |
Correct |
20 ms |
15856 KB |
Output is correct |
3 |
Correct |
18 ms |
15956 KB |
Output is correct |
4 |
Correct |
19 ms |
15980 KB |
Output is correct |
5 |
Correct |
18 ms |
15872 KB |
Output is correct |
6 |
Correct |
19 ms |
15944 KB |
Output is correct |
7 |
Correct |
18 ms |
15904 KB |
Output is correct |
8 |
Correct |
18 ms |
15968 KB |
Output is correct |
9 |
Correct |
19 ms |
15956 KB |
Output is correct |
10 |
Correct |
19 ms |
15928 KB |
Output is correct |
11 |
Correct |
20 ms |
16076 KB |
Output is correct |
12 |
Correct |
19 ms |
15880 KB |
Output is correct |
13 |
Correct |
18 ms |
15956 KB |
Output is correct |
14 |
Correct |
19 ms |
15956 KB |
Output is correct |
15 |
Correct |
19 ms |
15960 KB |
Output is correct |
16 |
Correct |
18 ms |
15888 KB |
Output is correct |
17 |
Correct |
19 ms |
15956 KB |
Output is correct |
18 |
Correct |
19 ms |
15980 KB |
Output is correct |
19 |
Correct |
19 ms |
15920 KB |
Output is correct |
20 |
Correct |
19 ms |
15928 KB |
Output is correct |
21 |
Correct |
18 ms |
15960 KB |
Output is correct |
22 |
Correct |
18 ms |
15956 KB |
Output is correct |
23 |
Correct |
20 ms |
16052 KB |
Output is correct |
24 |
Correct |
19 ms |
15952 KB |
Output is correct |
25 |
Correct |
19 ms |
15960 KB |
Output is correct |
26 |
Correct |
20 ms |
15956 KB |
Output is correct |
27 |
Correct |
19 ms |
15940 KB |
Output is correct |
28 |
Correct |
20 ms |
15956 KB |
Output is correct |
29 |
Correct |
18 ms |
15972 KB |
Output is correct |
30 |
Correct |
19 ms |
15980 KB |
Output is correct |
31 |
Correct |
19 ms |
15968 KB |
Output is correct |
32 |
Correct |
19 ms |
15952 KB |
Output is correct |
33 |
Correct |
19 ms |
15952 KB |
Output is correct |
34 |
Correct |
20 ms |
15956 KB |
Output is correct |
35 |
Correct |
26 ms |
15976 KB |
Output is correct |
36 |
Correct |
27 ms |
15956 KB |
Output is correct |
37 |
Correct |
27 ms |
15884 KB |
Output is correct |
38 |
Correct |
19 ms |
15984 KB |
Output is correct |
39 |
Correct |
21 ms |
15916 KB |
Output is correct |
40 |
Correct |
23 ms |
15864 KB |
Output is correct |