CF-weekly4 F. Kyoya and Colored Balls
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are labeled from 1 to k. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color ibefore drawing the last ball of color i + 1 for all i from 1 to k - 1. Now he wonders how many different ways this can happen.
The first line of input will have one integer k (1 ≤ k ≤ 1000) the number of colors.
Then, k lines will follow. The i-th line will contain ci, the number of balls of the i-th color (1 ≤ ci ≤ 1000).
The total number of balls doesn't exceed 1000.
A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1 000 000 007.
3
2
2
1
3
4
1
2
3
4
1680
In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are:
1 2 1 2 3
1 1 2 2 3
2 1 1 2 3
题意:
一共有n种颜色的球,同种颜色的球视为完全相同,设置一种放置规则,即上一种颜色球的最后一个必须放在当前颜色球最后一个的前面,问放球的方案数。
题解:
把情况分开考虑,一种颜色一种颜色的往上放,那么dp[i]表示已经放了i种颜色的方案数,a[i]表示第i种颜色的球的个数,sum[i]表示前i种球一共有这么多个,所以当放入i-1种颜色的球后,要把第i种球放一个到最后面以满足题意,然后向前插球,即当前一共有sum[i-1]+a[i]-1个位置,要在这些位置中找a[i]-1个位置放剩下的第i种球,那么这种操作的方法数就是c[sum[i-1+a[i]-1]][a[i]-1]
#include<iostream> #include<cstdio> #include<cstring> #define maxn 1010 #define mod 1000000007 using namespace std; int n,a[maxn],c[maxn][maxn],dp[maxn],sum[maxn]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); sum[i]=sum[i-1]+a[i]; } c[0][0]=1; for(int i=1;i<=1000;i++){ c[i][0]=1; for(int j=1;j<=1000;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod; } dp[0]=1; for(int i=1;i<=n;i++) dp[i]=1LL*dp[i-1]*c[a[i]-1+sum[i-1]][a[i]-1]%mod; printf("%d\n",dp[n]); return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!