AtCoder Beginner Contest 156 C.Rally

AtCoder Beginner Contest 156 C.Rally

Problem Statement

Akari has n kinds of flowers, one of each kind.
She is going to choose one or more of these flowers to make a bouquet.
However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.
How many different bouquets are there that Akari can make?

Find the count modulo ( 1 0 9 + 7 ) . (10^9+7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

  • All values in input are integers.
  • 2 n 1 0 9 2≤n≤10^9
  • 1 a < b m i n ( n , 2 × 1 0 5 ) 1≤a<b≤min(n,2×10^5)

Input

Input is given from Standard Input in the following format:

n  a  b

Output

Print the number of bouquets that Akari can make, modulo ( 1 0 9 + 7 ) (10^9+7) . (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

这题不难,首先推出所有情况为 2 n 1 2^n-1 ,减去组合数 C n a C_n^a C n b C_n^b 即可,套一个卢卡斯定理的模板,注意负数取模的问题,不能直接 ( a n s + m o d ) % m o d (ans+mod)\%mod ,否则会WA,要一直加直到大于0时才取模,具体见代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const ll mod=1e9+7;
ll p(ll a, ll n)
{
    if(n == 0) return 1;
    ll x = p(a, n/2);
    ll ans = x * x % mod;
    if(n % 2 == 1) ans = ans *a % mod;
    return ans%mod;
}

ll C(ll n,ll m) {
    if(n < m) return 0;
    ll res = 1;
    for(ll i=1; i<=m; i++) {
        ll a = (n+i-m)%mod;
        ll b = i%mod;
        res = res*(a*p(b,mod-2)%mod)%mod;
    }
    return res;
}

ll Lucas(ll n,ll m) {
    if(m == 0) return 1;
    return C(n%mod, m%mod) * Lucas(n/mod,m/mod)%mod;
}

ll n,a,b;
int main(){
    cin>>n>>a>>b;
    ll ans=p(2,n)-Lucas(n,a)-Lucas(n,b)-1;
   while(ans<0) {
        ans+=mod;
    }
    cout<<ans%mod;
    return 0;
}
发布了296 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/104455266
今日推荐