Helvetic Coding Contest 2018 online mirror (teams allowed, unrated) C2.Encryption (medium)

传送门:Encryption(medium)


题意:N个数,分成k个连续的序列,把每个序列的数累加取模p,求k个取模结果累加的最大值。

分析:DP, 复杂度O(Nkp)

设dp[i][j]为取模结果i,分为j个区间的最大值,具体见代码~


#include 
#include 
#include 
using namespace std;

const int INF = 1e9;
const int maxn = 20000+5;
const int maxp = 100+5;
const int maxk = 55;
int dp[maxp][maxk];
int a[maxn];
int n,k,p;

int main(){
    scanf("%d%d%d",&n,&k,&p);
    for (int i=0; i<=p; i++)
        for (int j=0; j<=k; j++) dp[i][j] = -INF;
    dp[0][0] = 0;
    for (int i=1; i<=n; i++) scanf("%d",&a[i]), a[i] %= p;

    int sum = 0;
    for (int i=1; i<=n; i++) {
        sum = (sum+a[i])%p;
        for (int j=1; j<=k; j++) {
            for (int f=0; f

你可能感兴趣的:(Codeforces)