AGC002F - Leftmost Ball(dp,组合计数)

AGC002F - Leftmost Ball

Solution

f i , j f_{i,j} fi,j表示放 i i i个白球,确定了 j j j个颜色的球的位置的方案数。

有两种转移:

  1. 放白球, f i , j − > f i + 1 , j f_{i,j}->f_{i+1,j} fi,j>fi+1,j
  2. 放完一种颜色的球, f i , j − > f i , j + 1 f_{i,j}->f_{i,j+1} fi,j>fi,j+1,方案数为 ( n − j ) ∗ ( n k − i − j ∗ ( k − 1 ) − 1 k − 2 ) (n-j)*\binom{nk-i-j*(k-1)-1}{k-2} (nj)(k2nkij(k1)1)

相当于当前情况下,没选过的第一个必选以保证方案不重复。

时间复杂度 O ( n 2 ) O(n^2) O(n2)

Code

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=1e9+7;
const int MAXN=2005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
int fac[MAXN*MAXN],inv[MAXN*MAXN],f[MAXN][MAXN];
int upd(int x,int y) { return x+y>=mods?x+y-mods:x+y; }
int C(int x,int y) { return 1ll*fac[x]*inv[y]%mods*inv[x-y]%mods; }
int quick_pow(int x,int y)
{
	int ret=1;
	for (;y;y>>=1)
	{
		if (y&1) ret=1ll*ret*x%mods;
		x=1ll*x*x%mods;
	}
	return ret;
}
void Init(int n)
{
	fac[0]=1;
	for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%mods;
	inv[n]=quick_pow(fac[n],mods-2);
	for (int i=n-1;i>=0;i--) inv[i]=1ll*inv[i+1]*(i+1)%mods;
}
signed main()
{
	int n=read(),k=read();
	if (k==1) { puts("1"); return 0; }
	Init(n*k),f[0][0]=1;
	for (int i=0;i<=n;i++) 
		for (int j=0;j<=i;j++)
		{
			if (i<n) f[i+1][j]=upd(f[i+1][j],f[i][j]);
			if (j<i) f[i][j+1]=upd(f[i][j+1],1ll*f[i][j]*(n-j)%mods*C(n*k-j*(k-1)-i-1,k-2)%mods);
		}
//	for (int i=0;i<=n;i++)
//		for (int j=0;j<=n;j++) cout<<i<<" "<<j<<":"<<f[i][j]<<endl;
	printf("%d\n",f[n][n]);
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴为您提供帮助!以下是一段可以生成任意简单多边形的代码: ```python import random def generate_polygon(num_vertices, min_coord, max_coord): # 生成随机的顶点坐标 vertices = [(random.randint(min_coord, max_coord), random.randint(min_coord, max_coord)) for i in range(num_vertices)] # 按照 x 坐标排序 vertices.sort() # 找到最左边的点和最右边的点 leftmost = vertices[0] rightmost = vertices[-1] # 将顶点分成上下两部分 uppers = [leftmost] lowers = [leftmost] for i in range(1, num_vertices): if i == num_vertices - 1 or (vertices[i][0] - leftmost[0]) * (rightmost[1] - leftmost[1]) > (rightmost[0] - leftmost[0]) * (vertices[i][1] - leftmost[1]): # 如果当前点在最左边的点和最右边的点之上,或者已经是最右边的点了,就将其加入上部分 while len(uppers) >= 2 and (vertices[i][0] - uppers[-2][0]) * (uppers[-1][1] - uppers[-2][1]) > (uppers[-1][0] - uppers[-2][0]) * (vertices[i][1] - uppers[-2][1]): uppers.pop() uppers.append(vertices[i]) elif (vertices[i][0] - leftmost[0]) * (rightmost[1] - leftmost[1]) < (rightmost[0] - leftmost[0]) * (vertices[i][1] - leftmost[1]): # 如果当前点在最左边的点和最右边的点之下,就将其加入下部分 while len(lowers) >= 2 and (vertices[i][0] - lowers[-2][0]) * (lowers[-1][1] - lowers[-2][1]) < (lowers[-1][0] - lowers[-2][0]) * (vertices[i][1] - lowers[-2][1]): lowers.pop() lowers.append(vertices[i]) # 将上下两部分连接起来 polygon = uppers + lowers[1:-1][::-1] return polygon # 生成一个 10 个顶点的简单多边形,坐标范围在 -100 到 100 之间 polygon = generate_polygon(10, -100, 100) print(polygon) ``` 这段代码使用了 Graham 扫描算法,可以生成任意简单多边形。具体实现过程可以参考这篇文章:https://www.geeksforgeeks.org/convex-hull-set-1-jarviss-algorithm-or-wrapping/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值