首页 > 其他分享 >Educational Codeforces Round 141 (Rated for Div. 2) Different Arrays

Educational Codeforces Round 141 (Rated for Div. 2) Different Arrays

时间:2023-01-09 22:22:37浏览次数:65  
标签:Educational Rated 141 300 Codeforces int solve 数组 Div


题意:

  • 给一个长度为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

相关文章

  • Educational Codeforces Round 141 (Rated for Div. 2) A-E
    比赛链接A题意给一个数组\(a\),要求重排列以后\(a[i]\neqa[1,i-1]\),其中\(a[1,i-1]\)是前\(i-1\)项和。如果无解则输出NO;否则,给出一个合法的重排列后的\(a......
  • leetcode简单:[66, 67, 70, 83, 121, 141, 160, 169, ,206, 338]
    目录66.加一67.二进制求和70.爬楼梯83.删除排序链表中的重复元素121.买卖股票的最佳时机141.环形链表160.相交链表169.多数元素206.反转链表338.比特位计数66.......
  • Educational Codeforces Round 141 (Rated for Div. 2) A-C题解
    比赛链接A、MakeitBeautiful一种构造方法:按照从大到小的顺序构造,可以发现前缀和永远大于当前值。但是需要特判存在两个最大值的情况。只需要将最小值与第二位交换位置......
  • CF--edu141--D
    关键DP,直接枚举当前这个位置的数可能的值,然后就可以递推出下一个位置可能的值,直接相加或者减去就可以了。因为当前的这个值可能是负数,所以需要加上一个偏移PS:其实我又......
  • Educational Codeforces Round 141
    A.MakeitBeautiful他想要变美,我们按照题目说的做就行,通过判断我们发现如果在sort一遍后sort(a+1,a+1+n);if(a[1]==a[n]){cout<<"NO"<<"\n";......
  • P1141 01迷宫
    这题数据有点高级啊(这么高级的数据能不能把它变成黄题呢?不然显得我很垃圾(虽然是事实))思路联通块,把周围四格与自己不同的联通起来,看成一个大块,知道要的坐标属于哪个大块并......
  • Educational Codeforces Round 14
    EducationalCodeforcesRound14https://codeforces.com/contest/6914/6:ABCD(C是恶心人的模拟分类讨论,写了巨久导致没时间看EF)这场没有红题,应该是可以补完的。A.Fas......
  • Educational Codeforces Round 13
    EducationalCodeforcesRound13https://codeforces.com/contest/6784/6:ABCD(1h)前4题都很简单,E应该是个撞鸭dp但是我想不出来A.JohnyLikesNumbers#include<bits/......
  • 1.6 vp Polynomial Round 2022 (Div. 1 + Div. 2, Rated, Prizes!)
    A-AddPlusMinusSign题意:给出01字符串,可以在每两个字符中间任意添加‘+’,‘-’。最后要使表达式的绝对值最小思路:设表达式的值为\(cnt\),若当前\(cnt\)大于\(0\),不管......
  • Educational Codeforces Round 11
    EducationalCodeforcesRound11https://codeforces.com/contest/660A.Co-primeArray\(1\)与任何数的\(gcd\)都为\(1\),直接在不符合条件的两点间塞就可以了#inc......