B. Alice and the List of Presents(组合数学)

B. Alice and the List of Presents

题意:给你n个礼物,m个盒子,让你往盒子里放礼物。
放礼物的规则:所有礼物必须出现,每个盒子的礼物必须不同,盒子允许为空。

题解:组合数学

我们先来看其中一个礼物a,
m个盒子放一个a:C(1,m) ;
m个盒子放两个a: C (2,m);
m个盒子放三个a: C (3,m);

m个盒子放m个a: C (m,m);

每个礼物可以放的总数为pow(2,m)-1 组合数学公式:pow(2,n);

一共有n个礼物:答案( pow(2,m)-1 )的n次方。

其中pow为指数幂函数。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll; 
const int maxn=1e5+10;
const int mod=1e9+7; 
pair<ll,ll> p[maxn];
pair<ll,ll>	q[maxn];
ll quick_pow(ll a,ll b)
{
	ll ans=1,temp=a;
	while(b)
	{
		if(b&1)	
			ans=(ans*temp)%mod;
		temp=(temp*temp)%mod;
		b>>=1;
	}
	return ans%mod;
}
int main()
{
	ios::sync_with_stdio(false);
	ll n,m;
	while(cin>>n>>m)
	{
		cout<<quick_pow((quick_pow(2,m)-1)%mod,n)%mod<<endl;
	}
	return 0;
 } 
发布了88 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43667611/article/details/102638717
今日推荐