CodeForces - 1236B Alice and the List of Presents(组合数学+快速幂)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1236B Alice and the List of Presents(组合数学+快速幂)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:愛麗絲現在有n種禮物,每種禮物有無限種,現在有m個盒子,需要向里面放禮物,滿足以下兩個條件:
問方案數對1e9+7取模
題目分析:一開始我的思路方向就錯了,我是對于每個盒子有2^n種子集,然后再討論的m個盒子,發現不管怎么推都沒辦法滿足上面的第二個條件,后來看了題解后知道了,我們可以直接對于每一種禮物,討論在m中出現的情況,因為每一種禮物在m個盒子中要么出現,要么不出現,其出現的子集共有2^m種情況,因為每種禮物都必須出現,所以對于每一種禮物的空集是不合法的,我們需要在總的子集數中減一,隨后對于n個禮物滿足乘法原理,其方案數可以相乘,最終答案就是了,因為其中涉及到了減法,我們需要多模兩下,防止帶著復數運算導致答案錯誤
代碼:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;const int mod=1e9+7;LL q_pow(LL a,LL b)//簡單的快速冪 {LL ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);LL a,b;scanf("%lld%lld",&a,&b);printf("%lld\n",q_pow(((q_pow(2,b)-1)%mod+mod)%mod,a));//日常模一模return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1236B Alice and the List of Presents(组合数学+快速幂)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gym - 101775J Straig
- 下一篇: CodeForces - 1248E Q