码迷,mamicode.com
首页 > 其他好文 > 详细

Tenka1 Programmer Contest 2019 D - Three Colors

时间:2019-05-03 16:23:51      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:size   space   iii   with   push   using   ret   注意   class   

Three Colors

思路:dp

设sum为所有边的总和

不能组成三角形的情况:某条边长度>=ceil(sum/2),可以用dp求出这种情况的方案数,然后用总方案数减去就可以求出答案。

注意当某两条边都为sum/2的时候,dp会多算一次,要减去多算的方案数,多算的方案数也可以用dp求

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
//#define mp make_pair
#define pb push_back
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pdi pair<double, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head

const int N = 305, M = 9e4 + 10;
const int MOD = 998244353;
int a[N], dp[N][M], pp[M], n, s = 0;
LL q_pow(LL n, LL k) {
    LL res = 1;
    while(k) {
        if(k&1) res = (res * n) % MOD;
        n = (n * n) % MOD;
        k >>= 1;
    }
    return res;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), s += a[i];
    dp[0][0] = 1;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < M; ++j) {
            dp[i][j] = (2*dp[i-1][j]) % MOD;
        }
        for (int j = a[i]; j < M; ++j) {
            dp[i][j] = (dp[i][j] + dp[i-1][j-a[i]]) % MOD;
        }
    }
    pp[0] = 1;
    for (int i = 1; i <= n; ++i) {
        for (int j = M-1; j >= a[i]; --j) pp[j] = (pp[j] + pp[j-a[i]]) % MOD;
    }
    if(s%2 == 0)dp[n][s/2] = (dp[n][s/2]-pp[s/2]) % MOD;
    LL ans = q_pow(3, n);
    int up = (s+1)/2;
    for (int i = up; i < M; ++i) ans = (ans - dp[n][i]*3LL%MOD) % MOD;
    printf("%lld\n", (ans + MOD) % MOD);
    return 0;
}

 

Tenka1 Programmer Contest 2019 D - Three Colors

标签:size   space   iii   with   push   using   ret   注意   class   

原文地址:https://www.cnblogs.com/widsom/p/10805335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!