結果

問題 No.1001 注文の多い順列
ユーザー mgingin142857mgingin142857
提出日時 2020-02-28 23:17:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,898 bytes
コンパイル時間 1,844 ms
コンパイル使用メモリ 170,940 KB
実行使用メモリ 76,160 KB
最終ジャッジ日時 2024-04-21 20:43:36
合計ジャッジ時間 4,570 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
5,760 KB
testcase_01 AC 6 ms
5,632 KB
testcase_02 AC 6 ms
5,632 KB
testcase_03 AC 6 ms
5,888 KB
testcase_04 AC 6 ms
5,632 KB
testcase_05 AC 5 ms
5,888 KB
testcase_06 AC 6 ms
5,888 KB
testcase_07 AC 6 ms
5,760 KB
testcase_08 AC 6 ms
5,760 KB
testcase_09 AC 6 ms
5,632 KB
testcase_10 AC 6 ms
5,632 KB
testcase_11 AC 5 ms
5,632 KB
testcase_12 AC 6 ms
5,632 KB
testcase_13 AC 6 ms
5,760 KB
testcase_14 AC 6 ms
6,144 KB
testcase_15 AC 7 ms
6,272 KB
testcase_16 AC 7 ms
6,016 KB
testcase_17 AC 6 ms
6,144 KB
testcase_18 AC 91 ms
63,872 KB
testcase_19 AC 81 ms
57,856 KB
testcase_20 AC 54 ms
39,296 KB
testcase_21 AC 49 ms
35,968 KB
testcase_22 AC 106 ms
76,032 KB
testcase_23 WA -
testcase_24 AC 105 ms
75,520 KB
testcase_25 AC 105 ms
74,240 KB
testcase_26 AC 104 ms
72,960 KB
testcase_27 AC 106 ms
74,240 KB
testcase_28 AC 106 ms
74,496 KB
testcase_29 AC 91 ms
75,264 KB
testcase_30 AC 91 ms
72,064 KB
testcase_31 AC 92 ms
75,392 KB
testcase_32 AC 107 ms
76,160 KB
testcase_33 AC 119 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&&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)%mod; //i+1 in w 
        dp[i+1][j+1-w[i+1]]%mod;
      }
    }
    fin += w[i+1];
  } 
  
  cout<<dp[n][0]<<endl;
}
0