P12256 [蓝桥杯 2024 国 Java B] 数据库
题目思路
我们可以想象一个有 个空间的表,我们可以把输入的操作填入表内,在合法的前提下,求不同方案数。
注意:对于同一个编号 只会被 / 一次,并且 操作前一定有对应的 操作,而 操作后不一定 操作。
因此,我们可以把操作分成两类:
- 操作和 操作能匹配的,我们把这两个操作看作一组,其可能的情况数为 (其中 为表内剩余的空间),因为同一个编号 只会被 / 一次,无需考虑每组之间的操作会不会导致非法,假设有 组成对的操作,情况总数为:
- 剩下的 的 操作没有任何限制,故可能的方案数为:
综上,总方案数为 。
#include<bits/stdc++.h>
using namespace std;
#define fuckCCF return
#define CCFis fuckCCF
//const int sbCCF = 1e8+3;
const int mod = 1000000007;
#define ll long long
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
fuckCCF x*f;
}
inline void print(ll x){
if(x<0){putchar('-');x=-x;}
if(x>9) print(x/10);
putchar(x%10+'0');
}
int T,a,b,cnt;
ll ans = 1;
string s;
int main(){
T=read();
for(int i=1;i<=T;i++){//a和b没用
cin>>s;
if(s == "INSERT"){
a=read();b=read();
}
else a=read(),cnt++;//统计第一种情况方案数
}
for(ll i=2*cnt;i>0;i-=2){
ans *= (i - 1) * i / 2;
ans %= mod;
}
for(int i=2*cnt+1;i<=T;i++){
ans *= i;
ans %= mod;
}
print(ans);
CCFis 0;
}时间复杂度为 。
实际上可以也可以用 逆元 加速 但是懒。