Buy the Ticket

Buy the Ticket
参考:Buy the Ticket

上面博客中好像 n 和 m 的意思写反了,不过问题不大,反着输入就好了,题目中说 n 是 50 的人数,m 是 100 的人数,这里反了一下。另外还需要用到高精度。

公式(m 是 50 的人数,n 是 100 的人数):
\[ (\binom {m+n}{n}-\binom {m+n}{m+1})*m!*n! \]
化简得:
\[ \frac{(m+n)!*(m-n+1)}{m+1} \]
代码:

// Created by CAD on 2019/8/17.
#include <bits/stdc++.h>
using namespace std;
#define MAXN 9999
#define MAXSIZE 10000
#define DLEN 4
class BigNum
{
public:
    int a[MAXSIZE];
    int len;
public:
    BigNum(){len = 1;memset(a,0,sizeof(a));}
    BigNum(const int);
    BigNum &operator=(const BigNum &);
    friend ostream& operator<<(ostream&,  BigNum&);
    BigNum operator*(const BigNum &) const;
    BigNum operator/(const int  &) const;
};
ostream& operator<<(ostream& out,  BigNum& b)
{
    int i;
    cout << b.a[b.len - 1];
    for (i = b.len - 2 ; i >= 0 ; i--)
    {
        cout.width(DLEN);
        cout.fill('0');
        cout << b.a[i];
    }
    return out;
}
BigNum::BigNum(const int b)
{
    int c,d = b;
    len = 0;
    memset(a,0,sizeof(a));
    while (d > MAXN)
    {
        c = d - (d / (MAXN + 1)) * (MAXN + 1);
        d = d / (MAXN + 1);  a[len++] = c;
    }
    a[len++] = d;
}
BigNum & BigNum::operator=(const BigNum & n)
{
    len = n.len;
    memset(a,0,sizeof(a));
    for (int i = 0 ; i < len ; i++)
        a[i] = n.a[i];
    return *this;
}
BigNum BigNum::operator*(const BigNum & T) const
{
    BigNum ret;
    int i,j,up;
    int temp,temp1;
    for (i = 0 ; i < len ; i++)
    {
        up = 0;
        for (j = 0 ; j < T.len ; j++)
        {
            temp = a[i] * T.a[j] + ret.a[i + j] + up;
            if (temp > MAXN)
            {
                temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
                up = temp / (MAXN + 1);
                ret.a[i + j] = temp1;
            }
            else
            {
                up = 0;
                ret.a[i + j] = temp;
            }
        }
        if (up != 0)
            ret.a[i + j] = up;
    }
    ret.len = i + j;
    while (ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
    return ret;
}
BigNum BigNum::operator/(const int & b) const
{
    BigNum ret;
    int i,down = 0;
    for (i = len - 1 ; i >= 0 ; i--)
    {
        ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
        down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
    }
    ret.len = len;
    while (ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
    return ret;
}
BigNum jc[205];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    jc[0]=jc[1]=BigNum(1);
    for(int i=2;i<=200;++i)
        jc[i]=jc[i-1]*BigNum(i);
    int n=1,m=1,t=0;
    while(cin>>m>>n&&(n+m))
    {
        BigNum ans(1);
        if(m<n) ans=BigNum(0);
        else
            ans=jc[m+n]*BigNum(m+1-n)/(m+1);
        cout<<"Test #"<<++t<<":"<<endl<<ans<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/CADCADCAD/p/11367597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值