Gym - 101775A — Chat Group (数论&&逆元&&快速幂)

链接:https://cn.vjudge.net/contest/250637#problem/A

思路:

求解C(n,k)+C(n,k+1)+...+C(n,n) 

因为组合数的总和是2^n

所以转化成 2^n - C(n,0)+C(n,1)+...+C(n,k-1)

求组合数相除时会爆精度,所以用逆元处理。

 

#include 
#include 
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll quick(ll a,ll b)
{
    ll ans = 1;
    a%=mod;
    while(b)
    {
        if(b&1)
        {
            ans= ans*a%mod;
        }
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    ll n,k,i;
    int cas=1,t;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ll ans=quick(2,n)-1;   //2^n - C(n,0)
        ll cur =n;
        for(int i=1; i

 

你可能感兴趣的:(ACM解题记录,数学)