Alice and the List of Presents
题目描述
Alice got many presents these days. So she decided to pack them into boxes and send them to her friends.
There are $ n $ kinds of presents. Presents of one kind are identical (i.e. there is no way to distinguish two gifts of the same kind). Presents of different kinds are different (i.e. that is, two gifts of different kinds are distinguishable). The number of presents of each kind, that Alice has is very big, so we can consider Alice has an infinite number of gifts of each kind.
Also, there are $ m $ boxes. All of them are for different people, so they are pairwise distinct (consider that the names of $ m $ friends are written on the boxes). For example, putting the first kind of present into the first box but not into the second box, is different from putting the first kind of present into the second box but not into the first box.
Alice wants to pack presents with the following rules:
- She won’t pack more than one present of each kind into the same box, so each box should contain presents of different kinds (i.e. each box contains a subset of $ n $ kinds, empty boxes are allowed);
- For each kind at least one present should be packed into some box.
Now Alice wants to know how many different ways to pack the presents exists. Please, help her and calculate this number. Since the answer can be huge, output it by modulo $ 10^9+7 $ .
See examples and their notes for clarification.
输入格式
The first line contains two integers $ n $ and $ m $ , separated by spaces ( $ 1 \leq n,m \leq 10^9 $ ) — the number of kinds of presents and the number of boxes that Alice has.
输出格式
Print one integer — the number of ways to pack the presents with Alice’s rules, calculated by modulo $ 10^9+7 $
样例 #1
样例输入 #1
1 3
样例输出 #1
7
样例 #2
样例输入 #2
2 2
样例输出 #2
9
题面翻译
有 n n n 种物品和 m m m 个背包,每种物品有无限个,现将若干个物品放到这些背包中,满足:
-
每个背包里不能出现相同种类的物品(允许有空背包);
-
在所有的 m m m 个背包中,每种物品都出现过。
求方案数,对 1 0 9 + 7 10^9+7 109+7 取模。
解析
每一种物品都有放进去和不放进去两种选择,但是不能全部不放,也就是说要减去不放的情况。所以共有 $ 2^m-1 $ 种情况,一共有 n n n 种物品,总共就是 $ (2m-1)n $ 种情况。
注意点
- 不开
long long
见祖宗。 - 需使用快速幂。
代码
#include<bits/stdc++.h> // 万能头
using namespace std;
typedef long long ll; // 需开 long long
const int mod = 1000000007; // 取模值
ll n,m,ans;
ll kasumi(ll a,ll b){ // 快速幂
ll ret=1;
while(b){
if(b%2) ret=(ret*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ret;
}
int main(){
ios::sync_with_stdio(false); // 断开 cin scanf
cin.tie(NULL);cout.tie(NULL); // cin cout 加速
cin >> n >> m;
ans=kasumi(kasumi(2,m)-1+mod,n);
cout << ans;
return 0; // 好习惯
}