結果

問題 No.391 CODING WAR
ユーザー __NCAstar__NCAstar
提出日時 2020-06-28 22:04:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,970 ms / 2,000 ms
コード長 2,906 bytes
コンパイル時間 1,356 ms
コンパイル使用メモリ 160,836 KB
実行使用メモリ 66,012 KB
最終ジャッジ日時 2024-07-08 08:21:44
合計ジャッジ時間 31,741 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,169 ms
66,012 KB
testcase_01 AC 1,201 ms
65,908 KB
testcase_02 AC 1,196 ms
65,820 KB
testcase_03 AC 1,175 ms
66,008 KB
testcase_04 AC 1,174 ms
65,824 KB
testcase_05 AC 1,206 ms
65,960 KB
testcase_06 AC 1,181 ms
65,860 KB
testcase_07 AC 1,197 ms
65,788 KB
testcase_08 AC 1,174 ms
65,876 KB
testcase_09 AC 1,970 ms
65,924 KB
testcase_10 AC 1,814 ms
65,856 KB
testcase_11 AC 1,171 ms
65,820 KB
testcase_12 AC 1,175 ms
65,784 KB
testcase_13 AC 1,876 ms
65,872 KB
testcase_14 AC 1,830 ms
65,920 KB
testcase_15 AC 1,924 ms
65,960 KB
testcase_16 AC 1,600 ms
65,932 KB
testcase_17 AC 1,677 ms
65,924 KB
testcase_18 AC 1,532 ms
65,820 KB
testcase_19 AC 1,522 ms
65,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define ALL(x) x.begin(),x.end()
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pb push_back
#define fst first
#define snd second

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;

inline bool LT(double a,double b) { return !equals(a,b) && a < b; }
inline bool LTE(double a,double b) { return equals(a,b) || a < b; }

const ll mod = 1000000007LL;

long long extgcd(long long a,long long b,long long& x,long long& y)
{
  long long d = a;
  if(b != 0){
    d = extgcd(b,a%b,y,x);
    y -= (a/b)*x;
  }
  else
    x = 1,y = 0;
  return d;
}

long long mod_inv(long long a,long long m)
{
  long long x,y;
  extgcd(a,m,x,y);
  return (m+x%m)%m;
}

const int MAXnCk = 4000001;
ll fact[MAXnCk+1], fact_inv[MAXnCk+1];
void init_nCk(){
  fact[0] = fact_inv[0] = 1;
  REP(i,1,MAXnCk+1) {
    fact[i] = fact[i-1] * (ll)i % mod;
    fact_inv[i] = mod_inv(fact[i],mod);
  }
}

ll nCk(ll n,ll k){
  if( n < 0 || k < 0 || k > n ) return 0LL;
  if( n-k < k ) k = n-k;
  return fact_inv[k] * fact[n] % mod * fact_inv[n-k] % mod;
}

ll modmultiply(ll a,ll b) {
  ll c = mod;
  ll x = 0,y = a%c; 
  while(b > 0) {
    if(b%2 == 1) x = (x+y)%c; 
    y = (y*2)%c;
    b /= 2; 
  }
  return x%c;
}

ll modpow(ll x, ll y) {
    ll ret = 1;// ret = x^y%mod;
    while(y) {
        if(y&1)
            //ret = (ret*x)%mod;
            ret = modmultiply(ret, x);
        //x = (x*x)%mod;
        x = modmultiply(x, x);
        y >>= 1;
    }
    return ret;
}



ll N, M;




void solve() {
  if( M > N ) { puts("0"); return; }
  ll n = N;
  ll k = M;
  ll v2 = 0;
  REP(i,1,k) {
    ll coef1 = 1;
    if( !( i & 1 ) ) coef1 = -1;
    ll coef2 = nCk(k,i);
    ll coef3 = modpow(k-i,n);
    v2 = ( v2 + ( ( ( coef1 * coef2 ) % mod ) * coef3 ) % mod );
    while( v2 < 0LL ) ( v2 += mod ) %= mod;
    //cout << "i = " << i << ", " << coef1 << " x " << coef2 << " x " << coef3 << endl;
  }
  v2 = ( modpow(k,n) - v2 ) % mod;
  while( v2 < 0LL ) ( v2 += mod ) %= mod;
  cout << v2 << endl;
}

int main() {
  init_nCk();
  int n = 7;
  int k = 3;
  ll v1 = 0;
  /*
  REP(i,1,k+1) {
    //ll coef1 = modpow(-1,k-i);
    ll coef1 = 1;
    rep(_,k-i) coef1 = coef1 * -1;
    ll coef2 = nCk(k,i);
    ll coef3 = modpow(i,n);
    v1 = ( v1 + ( ( ( coef1 * coef2 ) % mod ) * coef3 ) % mod );
    cout << "i = " << i << ", " << coef1 << " x " << coef2 << " x " << coef3 << endl;
  }
  cout << "v1 = " << v1 << endl;
  ll v2 = 0;
  REP(i,1,k) {
    ll coef1 = 1;
    if( !( i & 1 ) ) coef1 = -1;
    ll coef2 = nCk(k,i);
    ll coef3 = modpow(k-i,n);
    v2 = ( v2 + ( ( ( coef1 * coef2 ) % mod ) * coef3 ) % mod );
    cout << "i = " << i << ", " << coef1 << " x " << coef2 << " x " << coef3 << endl;
  }
  v2 = modpow(k,n) - v2;
  cout << "v2 = " << v2 << endl;
  */
  cin >> N >> M;
  solve();
  return 0;
}
0