結果

問題 No.391 CODING WAR
ユーザー sashimingsashiming
提出日時 2020-01-21 01:26:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,529 bytes
コンパイル時間 1,961 ms
コンパイル使用メモリ 198,416 KB
実行使用メモリ 5,496 KB
最終ジャッジ日時 2023-09-15 22:12:41
合計ジャッジ時間 3,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
5,212 KB
testcase_01 AC 30 ms
5,224 KB
testcase_02 AC 30 ms
5,268 KB
testcase_03 AC 31 ms
5,396 KB
testcase_04 AC 31 ms
5,228 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 30 ms
5,224 KB
testcase_07 AC 31 ms
5,216 KB
testcase_08 AC 30 ms
5,268 KB
testcase_09 AC 48 ms
5,272 KB
testcase_10 AC 48 ms
5,216 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 30 ms
5,220 KB
testcase_13 AC 45 ms
5,264 KB
testcase_14 AC 43 ms
5,248 KB
testcase_15 AC 45 ms
5,328 KB
testcase_16 AC 39 ms
5,272 KB
testcase_17 AC 41 ms
5,496 KB
testcase_18 AC 38 ms
5,268 KB
testcase_19 AC 37 ms
5,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i=0;i<(n);++i)
#define RANGE(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(n)-1;i>= 0;--i)

#define endl '\n'
#define pb push_back
#define eb emplace_back
#define pque priority_queue
#define umap unordered_map
#define uset unordered_set
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.141592653589793238
#define dcout cout<<fixed<<setprecision(50)
#define _popcnt __builtin_popcount

const long long dx[]={1,0,-1,0,1,1,-1,-1}, dy[]={0,-1,0,1,1,-1,1,-1};
const long long MOD = 1e9+7;

// typedef long long ll;
#define int long long
#define double long double

template<typename T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template<typename T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template<typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template<typename T> inline T nHr(T a,T b){return nCr(a+b-1,b);}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

typedef pair<int, int> INTP;
typedef vector<int> INTV;
struct edge2 {int to, cost;};
struct edge3 {int from, to, cost;};

int powmod(int a, int p, int m){
  int res = 1, tmp = a;
  while(p != 0){
    if(p % 2)
      res = (res*tmp) % m;
    tmp = (tmp*tmp) % m;
    p /= 2;
  }
  return res;
}

int fact[120000], invfact[120000];

int invmod(int a, int p){ // a^-1 mod p
  int b = p, u = 1, v = 0;
  while(b){
    int t = a / b;
    a -= t*b; swap(a, b);
    u -= t*v; swap(u, v);
  }
  u %= p;
  if(u < 0) u += p;
  return u;
}

void factmod(int m){
  fact[0] = invfact[0] = 1;
  for(int i = 1; i < 120000; i++){
    fact[i] = (fact[i-1] * i) % m;
    invfact[i] = (invfact[i-1] * invmod(i, m)) % m;
  }
}

int nCrmod(int n, int r, int m){
  return (((fact[n] * invfact[r]) % m) * invfact[n-r]) % m;
}

signed main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);
 
  int N, K; cin >> N >> K;
  if(N < K){
    cout << 0 << endl;
    return 0;
  }
  factmod(MOD);
  int ans = 0;
  for(int emp = 1; emp < K; emp++){
    int num = nCrmod(K, emp, MOD) * powmod(K-emp, N, MOD);
    num %= MOD;
    if(emp & 1){
      ans += num; ans %= MOD;
    } else {
      ans += MOD;
      ans -= num; ans %= MOD;
    }
  }
  ans = powmod(K, N, MOD) + MOD - ans;
  ans %= MOD;
  cout << ans << endl;
}
0