結果

問題 No.1001 注文の多い順列
ユーザー lgswdnlgswdn
提出日時 2023-10-10 23:40:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,343 bytes
コンパイル時間 2,922 ms
コンパイル使用メモリ 200,148 KB
実行使用メモリ 49,740 KB
最終ジャッジ日時 2023-10-10 23:40:09
合計ジャッジ時間 5,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 2 ms
4,456 KB
testcase_15 AC 4 ms
4,780 KB
testcase_16 AC 2 ms
4,504 KB
testcase_17 AC 3 ms
4,372 KB
testcase_18 AC 58 ms
28,560 KB
testcase_19 AC 55 ms
27,368 KB
testcase_20 AC 38 ms
20,036 KB
testcase_21 AC 35 ms
19,040 KB
testcase_22 AC 71 ms
33,368 KB
testcase_23 AC 70 ms
32,600 KB
testcase_24 AC 68 ms
32,436 KB
testcase_25 AC 65 ms
31,824 KB
testcase_26 AC 113 ms
48,272 KB
testcase_27 AC 108 ms
46,920 KB
testcase_28 AC 105 ms
45,348 KB
testcase_29 AC 18 ms
16,672 KB
testcase_30 AC 19 ms
16,880 KB
testcase_31 AC 21 ms
17,452 KB
testcase_32 AC 14 ms
15,504 KB
testcase_33 AC 144 ms
49,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//vanitas vanitatum et omnia
#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128; 
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}      
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x)  {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x)  {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x)  {return (x==0?-1:__builtin_ctzll(x));}

#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii; 
typedef vector<int> vi;  
typedef vector<pii> vp; 
typedef tuple<int,int,int> tiii;
int read() {
  int x=0,w=1; char c=getchar(); 
  while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
  while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
  return x*w;
}

const int N=3005,mod=1e9+7;
int f[N][N],n,a[N],b[N],sa[N],sb[N],fac[N],ifac[N],ans;
int ksm(int x,int y,int res=1) {
  for(;y;y>>=1,x=x*x%mod) if(y%2==1) res=res*x%mod;
  return res;
}
void pre(int n) {
  fac[0]=ifac[0]=1;
  rep(i,1,n) fac[i]=fac[i-1]*i%mod;
  ifac[n]=ksm(fac[n],mod-2);
  per(i,n-1,1) ifac[i]=ifac[i+1]*(i+1)%mod;
}
int C(int x,int y) {
  if(x<0||y<0||x<y) return 0;
  else return fac[x]*ifac[y]%mod*ifac[x-y]%mod;
}

signed main() {
  n=read(); pre(n);
  rep(i,1,n) {
    int t=read(), x=read();
    if(t==0) a[x]++;
    else b[x-1]++;
  }
  rep(i,1,n) sa[i]=sa[i-1]+a[i];
  sb[0]=b[0]; rep(i,1,n) sb[i]=sb[i-1]+b[i];
  f[0][0]=1;
  rep(i,1,n) rep(j,0,sb[i]) {
    rep(k,0,min(j,b[i])) if(i-sa[i]-j>=0) {
      int r=fac[i-sa[i-1]-j+k]*ifac[i-sa[i]-j]%mod;
      f[i][j]=(f[i][j]+f[i-1][j-k]*r%mod*C(b[i],k))%mod;
    }
  }
  rep(x,0,sb[n]) {
    int r=fac[n-sa[n]-x]*ifac[n-sa[n]-sb[n]]%mod;
    int res=r*f[n][x]%mod;
    if(x&1) ans=(ans+mod-res)%mod;
    else ans=(ans+res)%mod;
  }
  printf("%lld\n",ans);
  return 0;
}
0