2019-07-21  2024-09-15    565 字  2 分钟

牛客-C881 E - ABBA

假如把A看成1,B看成-1。根据它的条件会发现,他们的前缀和满足一个规律:[-m, n]。然后枚举每个位置。

可以看作是直角坐标系,原点为0。

x轴正方向看作A的数量,值为左边位置的值+1,当超出范围时置为0。

y轴正方向看作B的数量,值为下边位置的值-1,当超出范围时置为0.

其他位置均只能从左侧或下侧走到,即值为左侧加下侧的总和,当超出范围时置为0.

Code
  1/**
  2 *    author: Akvicor
  3 *    created: 2019-07-21 09-37-22
  4**/
  5
  6#include <bits/stdc++.h>
  7
  8using namespace std;
  9
 10#ifdef DEBUG
 11#define FAST_IO 17
 12#else
 13#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
 14#define endl '\n'
 15#endif
 16
 17#define rep(i, n) for(int i = 0; i < (n); ++i)
 18#define reep(i, n) for(int i = 0; i <= (n); ++i)
 19#define lop(i, a, n) for(int i = a; i < (n); ++i)
 20#define loop(i, a, n) for(int i = a; i <= (n); ++i)
 21#define prec(x) fixed << setprecision(x)
 22#define ms(s, n) memset(s, n, sizeof(s))
 23#define all(v) (v).begin(), (v).end()
 24#define sz(x) ((int)(x).size())
 25#define pb push_back
 26#define mp make_pair
 27#define fi first
 28#define se second
 29#define MOD(x) const int MOD = (int)x + 7
 30#define MAXN(x) const int MAXN = (int)x + 7
 31
 32typedef long long LL;
 33typedef unsigned long long ULL;
 34typedef pair<int, int> PII;
 35typedef vector<int> VI;
 36typedef vector<PII> VII;
 37
 38namespace SOL{
 39
 40const double EPS = 1e-6;
 41const double PI = acos(-1.0);
 42const int INF = 0x3f3f3f3f;
 43const LL LINF = 0x7f7f7f7f7f7f7f7f;
 44
 45/**  >------- Akvicor's Solution -------< **/
 46
 47MOD(1e9);
 48MAXN(1e6);
 49
 50void mod(long long & n){ n %= (long long)MOD; }
 51void mod(int & n){ n %= MOD; }
 52
 53LL dp[3010][3010];
 54
 55void solve(){
 56    FAST_IO;
 57    
 58    int n, m;
 59    while(cin >> n >> m){
 60	reep(i, n+m) reep(j, n+m) dp[i][j] = 0;
 61	dp[0][0] = 1;
 62	reep(i, n+m) reep(j, n+m){
 63		if(i) dp[i][j] += dp[i-1][j];
 64		if(j) dp[i][j] += dp[i][j-1];
 65		//dp[i][j] %= MOD;
 66		mod(dp[i][j]);
 67		if(i-j > n || j-i > m) dp[i][j] = 0;
 68	}
 69	cout << dp[n+m][n+m] << endl;
 70    }
 71    
 72}
 73
 74/**  >----------------------------------< **/
 75
 76}
 77
 78int main(){
 79
 80#ifdef DEBUG
 81    int DEBUGCNT = 0;
 82    clock_t DEBUGstart, DEBUGfinish;
 83    double DEBUGduration;
 84    cout << ">------- Akvicor's Solution -------<" << endl;
 85DEBUGLOOP:
 86    DEBUGstart = clock();
 87#endif
 88
 89    SOL::solve();
 90
 91#ifdef DEBUG
 92    DEBUGfinish = clock();
 93    DEBUGduration = (double)(DEBUGfinish - DEBUGstart)*1000 / CLOCKS_PER_SEC;
 94    if(DEBUGduration > 0.07)
 95    cout << " --> Test: #" << DEBUGCNT << " time: " << fixed << setprecision(4) << DEBUGduration << " ms <--" << endl;
 96    ++DEBUGCNT;
 97    if(DEBUGCNT < 100) goto DEBUGLOOP;
 98    cout << ">----------------------------------<" << endl;
 99#endif
100
101    return 0;
102}

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处