博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AtCoder Beginner Contest 156D - Bouquet(Lacus)
阅读量:3953 次
发布时间:2019-05-24

本文共 2425 字,大约阅读时间需要 8 分钟。

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

Akari has nn 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 aa and bb, so the number of flowers in the bouquet cannot be aa or bb.

How many different bouquets are there that Akari can make?

Find the count modulo (109+7)(109+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≤1092≤n≤109
  • 1≤a<b≤min(n,2×105)1≤a<b≤min(n,2×105)

Input

Input is given from Standard Input in the following format:

nn aa bb

Output

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


Sample Input 1 Copy

Copy

4 1 3

Sample Output 1 Copy

Copy

7

In this case, Akari can choose 22 or 44 flowers to make the bouquet.

There are 66 ways to choose 22 out of the 44 flowers, and 11 way to choose 44, so there are a total of 77 different bouquets that Akari can make.


Sample Input 2 Copy

Copy

1000000000 141421 173205

Sample Output 2 Copy

Copy

34076506

Print the count modulo (109+7)(109+7).

【题意】

Akari 有 n 种不同的花,她可以选择其中一种或多种花做成花束。但是 Akari 不喜欢花的种数恰好为 a 或 b 的花束。求出她组合花的合法方案总数,对 10^9+7 取模。

应该是c(n,1)+......c(n,n),,

因为c(n,0)+...c(n,n)=2^n;

所以推出公式:ans=(2^n)-C(n,a)-C(n,b)-1

由于n,a,b均小于模数,所以直接算组合数即可,不需要Lucas定理。

代码复杂度为:O(min(m,n-m))

适用于在m比较小,或m与n比较接近的情况。

#include 
using namespace std;typedef long long ll;const ll MOD=1e9+7;ll n,a,b,ans;ll pow(ll a,ll n){ ll res=1ll; while(n) { if (n&1) res=res*a%MOD; a=a*a%MOD; n>>=1; } return res%MOD;}ll C(ll n,ll m){ if(n
>n>>a>>b; ans=((pow(2ll,n)-C(n,a)-C(n,b)-1)%MOD+MOD)%MOD; cout<
<

用Lacus做::

需要注意的是,当同余定理用于减法的时候尽量在减完之后再加上模数,防止出现负数;

例如(6-3)% 6 = 3;

用同余定理计算后的(6%6-3%6)% 6 =(0-3)% 6 = -3;

当加上模数后 (6%6-3%6+6)% 6 =(0-3+6)% 6 = 3;

#include 
using namespace std;typedef long long ll;const int p=1e9+7;ll qmi(ll a,ll k)//用到快速幂/逆元/组合公式{ ll res=1; while(k) { if(k&1) res=res*a%p; a=a*a%p; k>>=1; } return res;}ll C(int a,int b){ ll res=1; for(ll i=1,j=a;i<=b;i++,j--) { res=res*j%p; res=res*qmi(i,p-2)%p; } return res;}ll lucas(ll a,ll b){ if(a
>n>>a>>b; cout<<(qmi(2,n)-(lucas(n,a)%p+lucas(n,b)%p)%p-1+p)%p<

 

转载地址:http://rvyzi.baihongyu.com/

你可能感兴趣的文章
Python 3 之多线程研究
查看>>
APP第三方登录实现步骤
查看>>
KVO & KVC 比较 - KVC
查看>>
iOS-tableView联动
查看>>
iOS--Masonry解决 tableViewCell 重用时约束冲突
查看>>
git 与 svn 的主要区别!
查看>>
iOS-截屏,从相册选择图片,制作磨砂效果图片
查看>>
iOS-截取字符串中两个指定字符串中间的字符串
查看>>
数据库-数据库操作(使用FMDB)
查看>>
FMDB介绍以及在 swift 中的数据库操作
查看>>
iOS运行时机制(附Demo演练)
查看>>
宽字符串输出问题
查看>>
将整数转换为宽字符串
查看>>
在类中定义enum实现整数常量功能
查看>>
suse11通过安装最新内核可以上网的经验
查看>>
SUSE静态配置IP成功上网
查看>>
通过sleep让程序等待外部条件改变
查看>>
通过等待键盘输入让程序等待外部条件改变
查看>>
通过限制循环次数来避免死循环
查看>>
ADO连接字符串
查看>>