답안 #411172

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411172 2021-05-24T13:19:28 Z 반딧불(#7588) Asceticism (JOI18_asceticism) C++17
49 / 100
600 ms 1988 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
const ll MOD = 1000000007;

ll mpow(ll x, ll y){
    if(!y) return 1;
    if(y&1) return mpow(x, y-1) * x % MOD;
    ll tmp = mpow(x, y/2);
    return tmp * tmp % MOD;
}

ll n, k;
ll fact[100002], factInv[100002];
ll DP[100002];
ll ans;

ll comb(ll x, ll y){
    return fact[x] * factInv[y] % MOD * factInv[x-y] % MOD;
}

int main(){
    fact[0] = 1;
    for(int i=1; i<=100000; i++) fact[i] = fact[i-1] * i % MOD;
    factInv[100000] = mpow(fact[100000], MOD-2);
    for(int i=99999; i>=0; i--) factInv[i] = factInv[i+1] * (i+1) % MOD;

    scanf("%lld %lld", &n, &k);
    if(k+k>n) k = n+1-k;

    DP[1] = 1;
    for(int i=2; i<=k; i++){
        for(int j=i; j>0; j--){
            if((i-j)%2==0) DP[i] = (DP[i] + comb(i, j) * mpow(j, n)) % MOD;
            else DP[i] = (DP[i] + (MOD - comb(i, j)) * mpow(j, n)) % MOD;
        }
    }

    ans = DP[k];
    for(int i=k-1; i>=1; i--){
        ll weight = comb(n-i, k-i);
        if((k-i)%2==0) ans = (ans + weight * DP[i]) % MOD;
        else ans = (ans + (MOD - weight) * DP[i]) % MOD;
    }
    printf("%lld", ans);
}

Compilation message

asceticism.cpp: In function 'int main()':
asceticism.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     scanf("%lld %lld", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 2 ms 1868 KB Output is correct
4 Correct 2 ms 1868 KB Output is correct
5 Correct 2 ms 1868 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 3 ms 1796 KB Output is correct
8 Correct 3 ms 1864 KB Output is correct
9 Correct 2 ms 1860 KB Output is correct
10 Correct 3 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 2 ms 1868 KB Output is correct
4 Correct 2 ms 1868 KB Output is correct
5 Correct 2 ms 1868 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 3 ms 1796 KB Output is correct
8 Correct 3 ms 1864 KB Output is correct
9 Correct 2 ms 1860 KB Output is correct
10 Correct 3 ms 1868 KB Output is correct
11 Correct 3 ms 1868 KB Output is correct
12 Correct 2 ms 1740 KB Output is correct
13 Correct 2 ms 1868 KB Output is correct
14 Correct 3 ms 1740 KB Output is correct
15 Correct 2 ms 1740 KB Output is correct
16 Correct 2 ms 1868 KB Output is correct
17 Correct 2 ms 1740 KB Output is correct
18 Correct 3 ms 1868 KB Output is correct
19 Correct 2 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 2 ms 1868 KB Output is correct
4 Correct 2 ms 1868 KB Output is correct
5 Correct 2 ms 1868 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 3 ms 1796 KB Output is correct
8 Correct 3 ms 1864 KB Output is correct
9 Correct 2 ms 1860 KB Output is correct
10 Correct 3 ms 1868 KB Output is correct
11 Correct 3 ms 1868 KB Output is correct
12 Correct 2 ms 1740 KB Output is correct
13 Correct 2 ms 1868 KB Output is correct
14 Correct 3 ms 1740 KB Output is correct
15 Correct 2 ms 1740 KB Output is correct
16 Correct 2 ms 1868 KB Output is correct
17 Correct 2 ms 1740 KB Output is correct
18 Correct 3 ms 1868 KB Output is correct
19 Correct 2 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
21 Correct 3 ms 1868 KB Output is correct
22 Correct 3 ms 1740 KB Output is correct
23 Correct 12 ms 1868 KB Output is correct
24 Correct 14 ms 1832 KB Output is correct
25 Correct 3 ms 1868 KB Output is correct
26 Correct 3 ms 1868 KB Output is correct
27 Correct 2 ms 1740 KB Output is correct
28 Correct 3 ms 1740 KB Output is correct
29 Correct 3 ms 1868 KB Output is correct
30 Correct 2 ms 1740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 2 ms 1868 KB Output is correct
4 Correct 2 ms 1868 KB Output is correct
5 Correct 2 ms 1868 KB Output is correct
6 Correct 2 ms 1868 KB Output is correct
7 Correct 3 ms 1796 KB Output is correct
8 Correct 3 ms 1864 KB Output is correct
9 Correct 2 ms 1860 KB Output is correct
10 Correct 3 ms 1868 KB Output is correct
11 Correct 3 ms 1868 KB Output is correct
12 Correct 2 ms 1740 KB Output is correct
13 Correct 2 ms 1868 KB Output is correct
14 Correct 3 ms 1740 KB Output is correct
15 Correct 2 ms 1740 KB Output is correct
16 Correct 2 ms 1868 KB Output is correct
17 Correct 2 ms 1740 KB Output is correct
18 Correct 3 ms 1868 KB Output is correct
19 Correct 2 ms 1868 KB Output is correct
20 Correct 2 ms 1868 KB Output is correct
21 Correct 3 ms 1868 KB Output is correct
22 Correct 3 ms 1740 KB Output is correct
23 Correct 12 ms 1868 KB Output is correct
24 Correct 14 ms 1832 KB Output is correct
25 Correct 3 ms 1868 KB Output is correct
26 Correct 3 ms 1868 KB Output is correct
27 Correct 2 ms 1740 KB Output is correct
28 Correct 3 ms 1740 KB Output is correct
29 Correct 3 ms 1868 KB Output is correct
30 Correct 2 ms 1740 KB Output is correct
31 Correct 2 ms 1740 KB Output is correct
32 Correct 2 ms 1740 KB Output is correct
33 Execution timed out 1095 ms 1988 KB Time limit exceeded
34 Halted 0 ms 0 KB -