Gym 101775A - Chat Group - [简单数学题][2017 EC-Final Problem A]
题目链接:http://codeforces.com/gym/101775/problem/A
It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: since every 3 or more persons could make a chat group, there can be 42 different chat groups.
Given N persons in a dormitory, and every K or more persons could make a chat group, how many different chat groups could there be?
Input
The input starts with one line containing exactly one integer T which is the number of test cases.
Each test case contains one line with two integers N and K indicating the number of persons in a dormitory and the minimum number of persons that could make a chat group.
1 ≤ T ≤ 100.
1 ≤ N ≤ 10^9.
3 ≤ K ≤ 10^5.
Output
For each test case, output one line containing "Case #x: y" where x is the test case number (starting from 1) and y is the number of different chat groups modulo 1000000007.
Example
Input
1
6 3
Output
Case #1: 42
题意:
听说一个寝室六个人有七个群?但实际上如果六人寝里三个人及以上组成不同的群的话,可以组成 个群……
现在给出一个 人寝室,要求计算 人及以上的不同的群可以建几个?
题解:
又根据二项式展开可知
因此答案即为 。
运用累乘的方式计算 ,注意除法要使用逆元。
AC代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD=1000000007; ll n,k; ll fpow(ll a,ll b) { ll r=1,base=a%MOD; while(b) { if(b&1) r*=base,r%=MOD; base*=base; base%=MOD; b>>=1; } return r; } ll inv(ll a){return fpow(a,MOD-2);} int main() { int T; cin>>T; for(int kase=1;kase<=T;kase++) { scanf("%lld%lld",&n,&k); if(n<k) { printf("Case #%d: 0\n",kase); continue; } ll sum=1+n,tmp=n; for(ll i=1;i<=k-2;i++) { tmp=(((tmp*(n-i))%MOD)*inv(i+1))%MOD; sum=(sum+tmp)%MOD; } ll ans=(fpow(2,n)-sum+MOD)%MOD; printf("Case #%d: %d\n",kase,ans); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术