1236B - Alice and the List of Presents(思维)

传送门
思路:

可以把n件物品拆成一件一件来考虑;

对于每一件物品,要保证至少有一个是放在m个包中的某一个包里的,那么总的合法方案就是 2^m-1(对于每一个包放或者不放,最后减去全不放的情况);

那么总的答案 就是第一种的物品的方案 * 第二种物品的方案 * …… =(2^m-1) ^ n ;(要把每一种物品放置 组合成 n 种物品放置);

同时这种考虑方法也规避掉了一个包 放置多个同一种物品的情况;

AC代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=1e6+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
#define ls (i<<1)
#define rs (i<<1|1)
#define fi first
#define se second
#define mk make_pair
#define mem(a,b) memset(a,b,sizeof(a))
LL read()
{
    LL x=0,t=1;
    char ch;
    while(!isdigit(ch=getchar())) if(ch=='-') t=-1;
    while(isdigit(ch)){ x=10*x+ch-'0'; ch=getchar(); }
    return x*t;
}
LL fp(LL x,LL y)
{
    LL res=1;
    while(y)
    {
        if(y&1) res=res*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return res;
}
int main()
{
    LL n=read(),m=read();
    LL tmp=fp(2,m)-1;
    printf("%lld\n",fp(tmp,n) );
    return 0;
}
posted @ 2019-12-11 21:14  DeepJay  阅读(326)  评论(0编辑  收藏  举报