18-- school sophomore training camp 501-511NOIP

Portal: QAQQAQ

 

Question is intended: to give you a number n-$ $, it is split into at most $ k $ positive integers, such that the n-number is equal to $ $ integer and each a positive number not exceeding $ 4 $

Split sequence is disordered, but is taken every number of different programs (e.g., I want to split $ 1 $, $ 4 $ kinds of programs there, because $ 4 $ a "1" are different)

 

Ideas: still immortal title. . Out no hope at this stage, we are now talking about the gradual deepening of the practice of sub-section

 

40 points: violence, we split into a number of types of $ n $ * n $ number $ 4, then ran 01 backpack on it, to prevent the MLE, you can open the scroll, but pay attention to shift to the opposite of

#include<bits/stdc++.h>
using namespace std;
const int MOD=1000000009;
 
int dp[10001][21],n,k,a[40005];
 
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        if(k>n) k=n;
        if(n==0) break;
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=4*n;i++) a[i]=(i+3)/4;
        for(int i=1;i<=4*n;i++)
        {
            for(int j=min(i,k);j>=1;j--)
            {
                for(int t=n;t>=a[i];t--)
                {
                    dp[t][j]=(dp[t][j]+dp[t-a[i]][j-1])%MOD;
                }
            }
        }
        int ans=0;
        for(int i=1;i<=k;i++) ans=(ans+dp[n][i])%MOD;
        printf("%d\n",ans);
    }
    return 0;
}

60 min: When we consider the one-dimensional optimization of the transfer - that is the ID number of the enumeration dimensional optimized away

We classify the discussion of transfer:

1. If the number of columns before the transfer is not 1, then we can go one-time number is set out in the plus 1, and add a new 1 "of different" - that is added to the 1-t multiplied by C (4 , t) (2,3,4 ...... this will ensure that have a "different" of)

2. If there is one, then we put all the columns are added to a number of metastasis, so that no 1

We can establish $ dp [i] [j] [bl] $ $ I $ is and is, taking the number of $ $ J, if it contains a number of columns (this state is preferably provided appreciated)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1000000009;

ll dp[100001][21][2],n,k;

void add(ll &x,ll y)
{
    x+=y;
    if(x>=MOD) x-=MOD;
}
ll c4[5]={1,4,6,4,1};

int main()
{
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        if(k>n) k=n;
        if(n==0&&k==0) break;
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=1;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=min(i,k);j++)
            {
                if(i>j)
                {
                    add(dp[i][j][0],dp[i-j][j][0]);
                    add(dp[i][j][0],dp[i-j][j][1]);
                }
                for(ll t=1;t<=min(j,4LL);t++)
                    add(dp[i][j][1],dp[i-t][j-t][0]*c4[t]%MOD); 
            }
        }
        ll ans=0;
        for(ll i=1;i<=k;i++)
        {
            add(ans,dp[n][i][0]);
            add(ans,dp[n][i][1]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

Of course, to the back of the eightieth code more convenient, we consider the state of dimension reduction, we remove the last bl, the "plus one for all" and "to an array of Riga 1" two operations together

Considering the rapid matrix 80 is a power, the rest of us pressed into a two-dimensional (k relatively small, it can be pressed)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1000000009;

ll dp[1000001],n,k;

void add(ll &x,ll y)
{
    x+=y;
    if(x>=MOD) x-=MOD;
}
ll c4[5]={1,4,6,4,1};
ll id(ll x,ll y)
{
    return x*10+y;
}

int main()
{
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        if(k>n) k=n;
        if(n==0&&k==0) break;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=min(i,k);j++)
            {
                for(ll t=0; T <= min (J, 4LL); ++ T ) 
                    the Add (DP [ID (I, J)], DP [ID (I -j, JT)] * C4 [T]% the MOD); 
                     // let the original array jt number are incremented by 1, then add the t 1 
            } 
        } 
        LL ANS = 0 ;
         for (LL I = 1 ; I <= K; I ++ ) 
        { 
            the Add (ANS, DP [ID (n-, I)]); 
        } 
        the printf ( " % LLD \ n- " , ANS); 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/Forever-666/p/11291697.html