題目鏈接:https://codeforces.com/gym/102021/attachments
題意:假設有一個非負的長度爲的數列,那麼可以得到一個數列,。現在題目給出數列,問數列的組合方式有多少種。
解題心得:設數列的第一項爲,那麼有,根據的公式可以得到關於的一系列約束條件,需要記錄約束的最大值和最小值,最後的答案若則組合方案數爲,否則答案爲。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
int n;
int a[maxn];
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
ll minn = 0, maxx = 1e18, now = 0;
for (int i = 1; i <= n; i++) {
if (i & 1) {
now = a[i] - now;
maxx = min(maxx, now);
} else {
now = a[i] - now;
minn = max(minn, -now);
}
}
if (minn > maxx || minn < 0 || maxx < 0) cout << 0 << '\n';
else cout << maxx - minn + 1 << '\n';
return 0;
}