結果
| 問題 |
No.1001 注文の多い順列
|
| コンテスト | |
| ユーザー |
mugen_1337
|
| 提出日時 | 2020-02-29 04:17:19 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 65 ms / 2,000 ms |
| コード長 | 1,498 bytes |
| コンパイル時間 | 1,881 ms |
| コンパイル使用メモリ | 173,388 KB |
| 実行使用メモリ | 49,536 KB |
| 最終ジャッジ日時 | 2024-10-13 19:48:20 |
| 合計ジャッジ時間 | 3,680 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
ll dp[3001][3001];
ll fact[3001];
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
fact[0]=1;
for(ll i=1;i<3001;i++) fact[i]=fact[i-1]*i%mod;
int n;cin>>n;
vector<pair<ll,ll>> v(n);
ll cnt=0;
rep(i,n){
cin>>v[i].second>>v[i].first;
if(v[i].second==1) v[i].first--,cnt++;
}
sort(ALL(v));
dp[0][0]=1;
rep(i,n){
for(int j=0;j<=i;j++){
dp[i+1][j]+=dp[i][j]*max(v[i].first-(i-j),0ll);
dp[i+1][j]%=mod;
if(v[i].second){
dp[i+1][j+1]+=dp[i][j];
dp[i+1][j+1]%=mod;
}
}
}
ll ans=0;
rep(i,cnt+1){
if(i%2==0) ans+=dp[n][cnt-i]*fact[cnt-i]%mod;
else ans+=mod-dp[n][cnt-i]*fact[cnt-i]%mod;
ans%=mod;
}
// rep(i,n+1){
// rep(j,n+1){
// cout<<dp[i][j]<<' ';
// }cout<<endl;
// }
cout<<ans<<endl;
return 0;
}
mugen_1337