[Codeforces301E][DP]Yaroslav and Arrangements

本文探讨了在特定条件下,即数列长度在1到n之间,每个数值介于1到m之间,且能重组得到1到k个良好数列的优秀数列的计算方法。通过动态规划技巧,文章详细解析了如何设计状态转移方程,以高效地计算满足条件的数列总数。

翻译

如果一个数列相邻两项之差的绝对值均为 1(我们认为首项和末项也相 邻),并且首项是数列中最小的元素之一,那么我们称之为良好数列。 如果一个数列单调不降且长度在 1 到 n 之间,数列中每个数的值在 1 到 m 之间,且重排后能得到至少 1 个至多 k 个良好数列,那么我们称之为优秀数列。 给出 n、m、k,求优秀数列的个数。 答案对 1000000007 取模

题解

这题有点东西…
把首项看成1,破环成链,a[n+1]=1
如果在最大值为i的情况下,最后乘一个m−i+1m-i+1mi+1就好了
我们考虑如何设计状态
首先你至少要知道当前这个数列能组成多少个良好数列
从小往大加数
比如两个1之间如果有空格,他们之间至少要加一个2
所以我们再加入一个状态,当前必须要加多少个数
啊别忘了记录当前加入了多少个数
枚举当前加入的数的数量
设他为T
设至少要加K个数
显然,我们会有T-K个组合两两之间是空格
所以
f[i][j][k]−>f[i+1][j+T][T−K]f[i][j][k]->f[i+1][j+T][T-K]f[i][j][k]>f[i+1][j+T][TK]
等等,还没转移方案数
其实就相当于T个球要放入K个箱子中不允许有空
那不就是f[i][j][k][l]−>f[i+1][j+T][T−K][l∗CT−1K−1]f[i][j][k][l]->f[i+1][j+T][T-K][l*C_{T-1}^{K-1}]f[i][j][k][l]>f[i+1][j+T][TK][lCT1K1]
然后枚举到一个最大值记录答案即可

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
using namespace std;
inline int read()
{
    int f=1,x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
inline void pr1(int x){write(x);printf(" ");}
inline void pr2(int x){write(x);puts("");}
int n,m,K;
int C[105][105];
int f[2][105][105][105];//当前最大值 已经加了多少个数 还必须要加多少个数 方案数 
void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;}
int main()
{
    n=read();m=read();K=read();
    if(n==1)return puts("0"),0;
    C[0][0]=1;
    for(int i=1;i<=100;i++)
        for(int j=0;j<=100;j++)
        {
            C[i][j]=j?C[i-1][j]+C[i-1][j-1]:C[i-1][j];
            if(C[i][j]>K)C[i][j]=K+1;	
        }
    n++;
    int now=0,ans=0;f[now][0][1][1]=1;
    for(int i=0;i<=m;i++)
    {
        now^=1;int num=0;
        if(i)
        {
            for(int j=2;j<=n;j++)
                for(int k=1;k<=K;k++)
                    ad(num,f[now^1][j][0][k]);
            ad(ans,(LL)num*(m-i+1)%mod);
        }
        memset(f[now],0,sizeof(f[now]));
        for(int j=0;j<=n;j++)
            for(int k=1;k<=n;k++)
                for(int l=1;l<=K;l++)
                    if(f[now^1][j][k][l])
                        for(int t=k;t+j<=n;t++)
                            ad(f[now][j+t][t-k][min(K+1,l*C[t-1][k-1])],f[now^1][j][k][l]);
    }
    pr2(ans);
    return 0;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值