Submission #768812

#TimeUsernameProblemLanguageResultExecution timeMemory
768812raysh07Asceticism (JOI18_asceticism)C++17
100 / 100
27 ms16076 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...