TopCoder SRM 382 CharmingTicketsEasy 夜

这题的主要算法是DP 只在最后结果上用了一下容斥原理

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
#include<map>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>

//#define ull unsigned long long
#define ll long long

using namespace std;

const int INF=0x3f3f3f3f;
const ll LMOD=999983;
const double eps=1e-6;
const int N=55;
const int M=500;
ll dp[N][M];
int a[N];
class CharmingTicketsEasy
{
    public :
    int count(int n, string good)
    {
        int m=good.size();
        for(int i=0;i<m;++i)
        a[i]=good[i]-'0';
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=n;++i)
        for(int j=0;j<M;++j)
        for(int w=0;w<m;++w)
        {
            if(j-a[w]>=0)
            dp[i][j]=(dp[i][j]+dp[i-1][j-a[w]])%LMOD;
        }
        ll tmp=0;
        for(int j=0;j<M;++j)
        tmp=(tmp+dp[n][j]*dp[n][j])%LMOD;
        ll k1=0;
        for(int j=0;j<M;++j)
        k1=(k1+dp[n/2][j]*dp[n/2][j])%LMOD;
        ll k2=0;
        if((n&1)==1)
        {
            for(int j=0;j<M;++j)
            k2=(k2+dp[n-n/2][j]*dp[n-n/2][j])%LMOD;
        }else
        {
            k2=k1;
        }
        return  (int)((tmp+tmp-k1*k2%LMOD+LMOD)%LMOD);
    }
};

  

原文地址:https://www.cnblogs.com/liulangye/p/3107811.html