题意:
- 给一个长度为n的数组a,下标从2到n-1,每个位置执行一次操作
- 操作:设操作位置为i,$a_{i-1} += a_i, a_{i+1} -= a_i$,或者$a_{i-1} -= a_i, a_{i+1} += a_i$
- 问最终能得到多少个不同的数组a,数量模998244353
- 数据
思路:
- 看到这个数据,有动态规划的感觉,但是对状态定义感觉很麻烦,所以就没做了
- 状态定义:$f[i][j]$表示数组a上第i个位置,元素大小为j的方案数
- 状态转移:
- 当 j 不为0的时候
- $f[i+1][j + a[i+1]] = \sum_{k=-M}^{M} f[i][j]$
- $f[i+1][- j + a[i+1]] =\sum_{k=-M}^{M} f[i][j]$
- 当 j 为0的时候,如果按照上面的方法,显然多算了一次(因为0不会改变数组)
- $f[i+1][a[i+1]] = f[i][j]$
- 因为减法操作 j 的状态可能为负数,所以加上一个 M = 300*300,改变相对位置
- 数组大小:f[N][2*M],N=300,M=300*300
- 时间复杂度:O(N*M)
#include<bits/stdc++.h> #define debug1(a) cout<<#a<<'='<< a << endl; #define debug2(a,b) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<endl; #define debug3(a,b,c) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<endl; #define debug4(a,b,c,d) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<endl; #define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<" "<<#b<<" = "<<b<<" "<<#c<<" = "<<c<<" "<<#d<<" = "<<d<<" "<<#e<<" = "<<e<<endl; #define endl "\n" #define fi first #define se second //#define int long long using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; //#pragma GCC optimize(3,"Ofast","inline") //#pragma GCC optimize(2) const int N = 310,mod = 998244353,M = 300*300; int a[N]; int f[N][M*2 + 10]; void solve() { int n;cin >> n; for(int i = 1;i <= n;i ++)cin >> a[i]; f[2][a[2] + M] = 1; for(int i = 2;i <= n-1;i ++) { for(int j = -M;j <= M;j ++) { if(j) { if(j + a[i+1] + M >= 0 && j + a[i+1] + M <= 2*M)(f[i+1][j + a[i+1] + M] += f[i][j + M]) %= mod; if(-j + a[i+1] + M >= 0 && -j + a[i+1] + M <= 2*M)(f[i+1][-j + a[i+1] + M] += f[i][j + M]) %= mod; }else{ f[i+1][a[i+1] + M] = f[i][j + M]; } } } int ans = 0; for(int i = -M;i <= M;i ++) { (ans += f[n][i+M]) %= mod; } cout << ans << endl; } signed main() { /* ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int T = 1;//cin >> T; while(T--){ //puts(solve()?"YES":"NO"); solve(); } return 0; } /* */
标签:Educational,Rated,141,300,Codeforces,int,solve,数组,Div From: https://www.cnblogs.com/cfddfc/p/17038695.html