题目链接:https://codeforces.com/contest/1236/problem/B
题意:给你n种礼物和m个盒子,每个礼物有无限个。
现在要将礼物放进盒子里,遵循以下规则:
(1)每个盒子里每种礼物最多只有一个,盒子可以为空;
(2)每种礼物至少放一个。
问一共有多少中方案数,答案对1e9 + 7取模。
思路:
(1)考虑一种礼物,m个盒子的情况(样例一),那样每个盒子要么放一个礼物,要么不放,再减去所有盒子全部为空的情况,一共有2^m - 1种情况;
(2)考虑n种礼物,m个盒子的情况,每个礼物之间都是相互独立的,所以答案便是(2^m - 1) ^ n;
所以答案便是(2^m - 1) ^ n,跑个快速幂就好。
代码:
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
const int maxn = 1e3+ 7;
const int IMAX = 2e4 + 7;
const int mod = 1e9 + 7;
LL qpow(LL a,LL b) {
LL res = 1;
while(b) {
if(b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int main() {
LL n , m;
while(~scanf("%lld%lld",&n,&m)) {
printf("%lld\n",qpow(qpow(2LL,m) - 1LL , n));
}
return 0;
}