結果

問題 No.824 Many Shifts Hard
ユーザー beetbeet
提出日時 2019-04-27 13:56:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 2,148 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 202,836 KB
実行使用メモリ 225,152 KB
最終ジャッジ日時 2024-05-05 19:34:05
合計ジャッジ時間 7,858 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
225,152 KB
testcase_01 AC 172 ms
225,152 KB
testcase_02 AC 177 ms
225,152 KB
testcase_03 AC 201 ms
225,152 KB
testcase_04 AC 188 ms
225,152 KB
testcase_05 AC 247 ms
225,152 KB
testcase_06 AC 173 ms
225,152 KB
testcase_07 AC 180 ms
225,152 KB
testcase_08 AC 239 ms
225,152 KB
testcase_09 AC 214 ms
225,024 KB
testcase_10 AC 173 ms
225,152 KB
testcase_11 AC 179 ms
225,024 KB
testcase_12 AC 197 ms
225,024 KB
testcase_13 AC 229 ms
225,152 KB
testcase_14 AC 268 ms
225,152 KB
testcase_15 AC 255 ms
225,152 KB
testcase_16 AC 179 ms
225,152 KB
testcase_17 AC 267 ms
225,152 KB
testcase_18 AC 172 ms
225,024 KB
testcase_19 AC 173 ms
225,024 KB
testcase_20 AC 271 ms
225,024 KB
testcase_21 AC 174 ms
225,152 KB
testcase_22 AC 271 ms
225,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,T MOD = 1000000007>
struct Mint{
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }
  
  Mint inv(){return pow(MOD-2);}
  
  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}
  
  Mint operator+(Mint a) const{return Mint(v)+=a;};
  Mint operator-(Mint a) const{return Mint(v)-=a;};
  Mint operator*(Mint a) const{return Mint(v)*=a;};
  Mint operator/(Mint a) const{return Mint(v)/=a;};

  Mint operator-(){return v?MOD-v:v;}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}
};

//INSERT ABOVE HERE
const int MAX = 305;
using M = Mint<int>;
M dp[2][MAX][MAX][MAX];
signed main(){
  int n,k;
  cin>>n>>k;

  if(n==1){
    cout<<0<<endl;
    return 0;
  }
  
  // x != 1
  dp[0][0][0][1]=1;
  // x == 1
  dp[1][0][0][1]=1;
  
  for(int l=0;l<k;l++){
    for(int i=0;i<=k;i++){
      for(int j=i+1;j<=k+1;j++){
        if(i+1<j){
          dp[0][l+1][i+1][j]+=dp[0][l][i][j];
          dp[1][l+1][i+1][j]+=dp[1][l][i][j];
        }
        dp[0][l+1][i][j+1]+=dp[0][l][i][j];
        dp[1][l+1][i][j+1]+=dp[0][l][i][j];

        dp[0][l+1][i][j]+=dp[0][l][i][j]*M(n-2);   
        dp[1][l+1][i][j]+=dp[1][l][i][j]*M(n-1);  
      }
    }
  }
  
  M ans(0);
  {
    int l=k;
    for(int i=0;i<=k;i++){
      for(int j=i+1;j<=k+1;j++){
        if(j>n) break;
        ans+=dp[0][l][i][j]*M(1LL*((j-i+1)+(n-i))*(n-j)/2);
        ans+=dp[1][l][i][j]*M(j-i);        
      }
    }
  }  
  cout<<ans.v<<endl;
  return 0;
}
0