結果

問題 No.1001 注文の多い順列
ユーザー mgingin142857mgingin142857
提出日時 2020-02-28 23:35:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,905 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 170,488 KB
実行使用メモリ 76,032 KB
最終ジャッジ日時 2024-04-21 20:57:05
合計ジャッジ時間 4,239 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,812 KB
testcase_01 AC 5 ms
6,944 KB
testcase_02 AC 5 ms
6,940 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 6 ms
6,944 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 6 ms
6,940 KB
testcase_08 AC 5 ms
6,944 KB
testcase_09 AC 5 ms
6,944 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 6 ms
6,944 KB
testcase_12 AC 6 ms
6,944 KB
testcase_13 AC 6 ms
6,944 KB
testcase_14 AC 6 ms
6,944 KB
testcase_15 AC 7 ms
6,940 KB
testcase_16 AC 6 ms
6,940 KB
testcase_17 AC 6 ms
6,944 KB
testcase_18 AC 83 ms
63,744 KB
testcase_19 AC 73 ms
58,112 KB
testcase_20 AC 48 ms
39,296 KB
testcase_21 AC 44 ms
35,840 KB
testcase_22 AC 93 ms
76,032 KB
testcase_23 AC 94 ms
75,648 KB
testcase_24 AC 94 ms
75,520 KB
testcase_25 AC 92 ms
73,984 KB
testcase_26 AC 101 ms
72,832 KB
testcase_27 AC 101 ms
74,112 KB
testcase_28 AC 101 ms
74,368 KB
testcase_29 AC 84 ms
75,392 KB
testcase_30 AC 81 ms
72,192 KB
testcase_31 AC 84 ms
75,520 KB
testcase_32 AC 83 ms
76,032 KB
testcase_33 AC 105 ms
76,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
const int inf=1e9+7;
const ll INF=1LL<<61;
const ll mod=1e9+7;

template<class T>
inline bool chmax(T &a, T b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}

template<class T>
inline bool chmin(T &a, T b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}


vector<ll> inv,fact,invfact;
void Mod_build(int n=101010){
	fact.resize(n+1);
	inv.resize(n+1);
	invfact.resize(n+1);
	fact[0]=inv[0]=invfact[0]=1;
	inv[1]=1;
	for(ll i=0;i<n;i++){
		fact[i+1]=fact[i]*(i+1)%mod;
		if(i>0)inv[i+1]=mod-inv[mod%(i+1)]*(mod/(i+1))%mod;
		invfact[i+1]=invfact[i]*inv[i+1]%mod;
	}
}
ll perm(int n,int k){
	if(n<0||k<0||k>n)return 0;
	return fact[n]*invfact[n-k]%mod;
}
ll comb(int n,int k){
	if(n<0||k<0||k>n)return 0;
	return (fact[n]*invfact[n-k]%mod)*invfact[k]%mod;
}
ll powmod(ll n,ll k){
	k%=mod-1;
	if(k<0)k+=mod-1;
	ll ret=1;
	while(k){
		if(k&1)ret=ret*n%mod;
		n=n*n%mod;
		k>>=1;
	}
	return ret;
}

int main(){
  int i,j;
  int n;
  cin>>n;
  VI v(n+1,0),w(n+1,0);
  for(i=0;i<n;i++){
    int t,x;
    cin>>t>>x;
    if(t) v[x]++;
    else w[x]++;
  }
  Mod_build();
  ll ok=0;
  ll fin=0;
  VVI dp(n+1,VI(n+1,0));
  dp[0][0]=1;
  for(i=0;i<n;i++){
    ok += v[i+1];
    for(j=0;j<=i;j++){
      if(j-w[i+1]>=0&&i-j-fin>=0&&ok-i+j+fin>=0) {
        dp[i+1][j-w[i+1]]  += (fact[j]*invfact[j-w[i+1]]%mod)*((ok-i+j+fin)*dp[i][j]%mod)%mod; //i+1 in v 
        dp[i+1][j-w[i+1]]%mod;
      }
      if(j+1-w[i+1]>=0) {
        dp[i+1][j+1-w[i+1]]  += (fact[j+1]*invfact[j+1-w[i+1]]%mod)*dp[i][j]%mod; //i+1 in w 
        dp[i+1][j+1-w[i+1]]%mod;
      }
    }
    fin += w[i+1];
  } 
  cout<<dp[n][0]%mod<<endl;
}
0