组合数+逆元 A - Chat Group Gym - 101775A

题目链接:https://cn.vjudge.net/contest/274151#problem/A

具体思路:我们可以先把所有的情况算出来,为2^n.然后不合法的情况减去就可以了.注意除法的时候要用到逆元.

AC代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int maxn = 100000+100;
# define mod 1000000007
ll quickpow(ll t1,ll t2)
{
    t2--;
    ll ans=t1;
    while(t2)
    {
        if(t2&1)ans=ans*t1%mod;
        t1=t1*t1%mod;
        t2>>=1;
    }
    return ans;
}
ll inv(ll t)
{
    return quickpow(t,mod-2);
}
int main()
{
    int T;
    int Case=0;
    scanf("%d",&T);
    while(T--)
    {
        ll n,m;
        scanf("%lld %lld",&n,&m);
        ll t1=n,t2=n+1;
        ll temp=quickpow(2,n);
        if(n<m)
        {
            printf("Case #%d: %lld\n",++Case,0);
        }
        else
        {
            for(int i=2; i<=m-1; i++)
            {
                t1=t1*(n-i+1)%mod*inv(i)%mod;
                t2=(t2+t1)%mod;
            }
            printf("Case #%d: %lld\n",++Case,(temp-t2+mod)%mod);
        }
    }
    return 0;
}

 

posted @ 2018-12-07 09:04  Let_Life_Stop  阅读(123)  评论(0编辑  收藏  举报