結果

問題 No.1001 注文の多い順列
ユーザー leaf_1415leaf_1415
提出日時 2020-02-28 22:00:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 2,015 bytes
コンパイル時間 1,141 ms
コンパイル使用メモリ 70,932 KB
実行使用メモリ 77,324 KB
最終ジャッジ日時 2023-08-03 20:13:05
合計ジャッジ時間 4,663 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,620 KB
testcase_01 AC 6 ms
9,620 KB
testcase_02 AC 6 ms
9,468 KB
testcase_03 AC 6 ms
9,476 KB
testcase_04 AC 6 ms
9,528 KB
testcase_05 AC 6 ms
9,516 KB
testcase_06 AC 6 ms
9,536 KB
testcase_07 AC 6 ms
9,660 KB
testcase_08 AC 6 ms
9,540 KB
testcase_09 AC 6 ms
9,548 KB
testcase_10 AC 6 ms
9,592 KB
testcase_11 AC 7 ms
9,516 KB
testcase_12 AC 6 ms
9,440 KB
testcase_13 AC 7 ms
9,912 KB
testcase_14 AC 7 ms
10,364 KB
testcase_15 AC 8 ms
11,116 KB
testcase_16 AC 7 ms
10,312 KB
testcase_17 AC 7 ms
10,200 KB
testcase_18 AC 75 ms
53,460 KB
testcase_19 AC 71 ms
51,764 KB
testcase_20 AC 49 ms
38,308 KB
testcase_21 AC 45 ms
35,880 KB
testcase_22 AC 84 ms
58,148 KB
testcase_23 AC 82 ms
57,276 KB
testcase_24 AC 83 ms
57,364 KB
testcase_25 AC 81 ms
56,920 KB
testcase_26 AC 107 ms
77,324 KB
testcase_27 AC 106 ms
75,516 KB
testcase_28 AC 103 ms
73,876 KB
testcase_29 AC 72 ms
50,104 KB
testcase_30 AC 72 ms
51,120 KB
testcase_31 AC 77 ms
54,036 KB
testcase_32 AC 33 ms
21,196 KB
testcase_33 AC 119 ms
77,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#define llint long long
#define inf 1e18
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint t[3005], x[3005];
vector<P> vec;
llint dp[3005][3005];
const int FACT_MAX = 200005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < FACT_MAX; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2);
	for(int i = FACT_MAX-2; i >= 0; i--){
		fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
	}
}

llint comb(llint n, llint k)
{
	llint ret = 1;
	ret *= fact[n];
	ret *= fact_inv[k], ret %= mod;
	ret *= fact_inv[n-k], ret %= mod;
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	make_fact();
	
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> t[i] >> x[i];
		vec.push_back(P(x[i], t[i]));
		if(t[i] == 1) vec.back().first--;
	}
	sort(vec.begin(), vec.end());
	
	llint cnt = 0;
	dp[0][0] = 1;
	for(int i = 0; i < n; i++){
		llint x = vec[i].first, t = vec[i].second;
		//cout << x << " " << t << endl;
		for(int j = 0; j <= n; j++){
			if(t == 0){
				if(x-(cnt+j) >= 0){
					dp[i+1][j] += dp[i][j] * (x-(cnt+j)) % mod;
					dp[i+1][j] %= mod;
				}
			}
			else{
				dp[i+1][j] += dp[i][j], dp[i+1][j] %= mod;
				if(j+1 <= n && x-(cnt+j) >= 0){
					dp[i+1][j+1] += dp[i][j] * (x-(cnt+j)) % mod;
					dp[i+1][j+1] %= mod;
				}
			}
		}
		if(t == 0) cnt++;
	}
	
	/*for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			cout << dp[i][j] << " ";
		}
		cout << endl;
	}*/
	
	llint ans = 0;
	for(int i = 0; i <= n; i++){
		llint tmp = dp[n][i];
		tmp *= fact[n-(cnt+i)], tmp %= mod;
		if(i % 2) ans += mod - tmp, ans %= mod;
		else ans += tmp, ans %= mod;
	}
	cout << ans << endl;
	
	return 0;
}
0