BJ模拟(1) D1T1 计数

计数

题目背景:

thoj21

分析:

先考虑放置所有的A,要在所有的相邻的A中间进行插入,然后我们考虑插入B,那么之后就会形成..A..B..B..B..A..B..A..B的形式

可以通过枚举在多少的A之间插入了B来得到

1)若两个相邻的字符相同,则必须在之间插入一些东西

2)若相邻的两个字符不相同,则可以在它们之间插入一些东西

那么1的插入是必要的,我们直接枚举在多少个2)类区间中插入了东西,那么问题就变成了如何在n个区间中插入给定次数的C和D,显然,每个区间可能插入的类型有4种

1)C...DCDC

2) D...CDCD

3)CD...CDCD

4) DC...DCDC

枚举类型1)的出现次数,因为我们知道C和D的出现次数的差值是一定的,所以类型2的出现次数就业能够确定了,那么其余的就是3,4类型,(这两者并没有本质的区别,假设有x个区间是这两种类型,那么之间诶将答案*2x后就可以了,然后我们现在所有类型1的区间中插入一个C,在类型2的所有区间中插入一个D,在类型3,4的区间中插入一个CD或DC,之后每个区间都只能插入若干对CD,求出剩余的对数S,将相当于把S分配到n个框中,就变成了经典的组合问题了,因为我们发现n的范围不大,那么我们完全可以通过组合数递推,预处理可能会出现的组合数,所以,最后的时间复杂度应该为O(n2)

Source:

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           using namespace std; inline char read() { static const int IN_LEN = 1024 * 1024; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; } template 
          
            inline bool R(T &x) { static char c; static bool iosig; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == -1) return false; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = (x << 3) + (x << 1) + (c ^ '0'); if (iosig) x = -x; return true; } const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN], *oh = obuf; inline void writechar(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; *oh++ = c; } template 
           
             inline void W(T x) { static int buf[30], cnt; if (!x) writechar(48); else { if (x < 0) writechar('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) writechar(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); } const int MAXN = 2010; const int mod = 1e9 + 7; int C[MAXN][MAXN], f[MAXN], g[MAXN]; int a, b, c, d; inline void init(int a, int b, int *f) { if (a < b) swap(a, b); if (!a) return (void)(f[0] = 1); else if (!b) return (void)(f[a] = 1); for (int i = 0; i < 4; ++i) { int c = i + 1 >> 1; for (int j = 0; j <= min(b - c, a - 1); ++j) { int k = (a - 1 - j) + (b - c - j); int temp = 1LL * C[a - 1][j] * C[b - 1][j + c - 1] % mod; for (int l = k + 1; l <= a + b; ++l) f[l] = (f[l] + 1LL * C[a + b - 1 - k][l - k - 1] * temp) % mod; } } } int main() { // freopen("in.in", "r", stdin); R(a), R(b), R(c), R(d); for (int i = 0; i <= MAXN - 5; ++i) C[i][0] = C[i][i] = 1; for (int i = 0; i <= MAXN - 5; ++i) for (int j = 1; j <= i - 1; ++j) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; init(a, b, f), init(c, d, g); int ans = 0; for (int i = 0; i <= a + b; ++i) { ans = (ans + 2LL * f[i] * g[i] + 1LL * f[i] * g[i + 1]) % mod; if (i) ans = (ans + 1LL * f[i] * g[i - 1]) % mod; } ans = (ans % mod + mod) % mod; W(ans), flush(); return 0; } 
            
           
          
         
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值