洛谷P1287 盒子与球 数学

本文详细解析洛谷P1287题目——盒子与球的解决方案,利用第二类斯特林数计算将n个不同的球放入k个不同盒子中的方案数,并通过C++代码实现。

洛谷P1287 盒子与球 数学
第二类斯特林数
将 n 个 互不相同的球 放入 k 个互不相同的盒子中,且不能为空,求方案数

如果盒子相同的话用第二类斯特林数来做
s[n][k] 表示 将 n 个可区分的球 放进 k 个 不可区分的盒子中 的方案数
s[n][k] = s[n-1][k-1] + k*s[n-1][k]

s[n-1][k-1] 表示将 这个球单独列为 一份 ,也就是说这份里面只有一个求
s[n-1][k] 表示将 这个球放到其他份子中,然后总共 有 k个份子可以放

然后 现在 球盒互不相同了,那就乘以 m!好了

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std ; 
 5 
 6 inline int read() 
 7 {
 8     char ch = getchar() ; 
 9     int x = 0 ,f = 1 ;
10     while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ;ch = getchar() ; }
11     while(ch>='0'&&ch<='9') { x = x*10+ch-48 ; ch = getchar() ; }
12     return x*f ;
13 }
14 
15 int n,k,ans ;
16 int s[11][11] ;  
17 
18 inline int jiecheng(int n) 
19 {
20     int ans = 1 ;
21     for(int i=2;i<=n;i++) ans*=i ; 
22     return ans ; 
23 }
24 
25 int main() 
26 {
27     n = read() ;  k = read() ;
28     s[ 1 ][ 1 ] = 1 ; 
29     for(int i=2;i<=n;i++) 
30         for(int j=1;j<=i;j++) 
31             s[ i ][ j ] = s[i-1][j-1] + j*s[i-1][j] ;
32     ans = s[n][k] * jiecheng(k) ;  
33     printf("%d\n",ans) ; 
34     return 0 ; 
35 }

 

转载于:https://www.cnblogs.com/third2333/p/6943372.html

### 关于洛谷 P1918 保龄题目解析及解法 #### 题目背景描述 DL 对传统保龄感到厌倦,希望尝试一些新的玩法。为了实现这一目标,在该问题设定下,需要处理一系列特定条件下的保龄击倒情况。 #### 解题思路概述 对于此问题存在多种解决方案: - **使用 `map` 容器** 可以通过 C++ STL 中的 `map` 来存储每个位置对应的瓶子数量关系。当输入待查询的位置 m 后,能够迅速定位并返回对应的结果 mp[m] [^2]。 ```cpp #include <iostream> #include <map> using namespace std; int main() { int n, q; cin >> n >> q; map<int, int> bottlePositions; for (int i = 0; i < n; ++i) { int pos, num; cin >> pos >> num; bottlePositions[pos] = num; } while(q--) { int queryPos; cin >> queryPos; cout << bottlePositions[queryPos] << endl; } return 0; } ``` - **采用自定义结构体加二分查找** 另一种方式是构建一个包含两个成员变量(表示位置和瓶数)的结构体数组,并对其进行排序以便后续执行高效的二分查找操作 [^3]。 ```cpp #include <algorithm> #include <vector> #include <iostream> struct BottleInfo { int position; int count; }; bool compare(const BottleInfo& a, const BottleInfo& b){ return a.position < b.position; } // Binary search function to find the index of target value. int binarySearch(std::vector<BottleInfo>& vec, int key){ int low=0, high=(int)(vec.size())-1,mid; while(low<=high){ mid=(low+high)/2; if(vec[mid].position==key)return mid; else if(key<vec[mid].position)high=mid-1; else low=mid+1; } return -1; } int main(){ int N,Q; scanf("%d%d",&N,&Q); vector<BottleInfo> bottles(N); for(int i=0;i<N;++i){ scanf("%d %d", &bottles[i].position, &bottles[i].count); } sort(bottles.begin(), bottles.end(), compare); while(Q--){ int queryPosition; scanf("%d", &queryPosition); int idx=binarySearch(bottles, queryPosition); printf("%d\n",(idx != -1)?bottles[idx].count:-1); } return 0; } ``` 上述两种方法都能有效地解决问题,具体选择取决于个人偏好以及对性能的要求差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值