The 2018 ACM-ICPC China JiangSu Provincial Programming Contest - D. Persona5

Persona5 is a famous video game.

In the game, you are going to build relationship with your friends.

You have N friends and each friends have his upper bound of relationship with you. Let's consider the ith friend has the upper bound Ui​. At the beginning, the relationship with others are zero. In the game, each day you can select one person and increase the relationship with him by one. Notice that you can't select the person whose relationship with you has already reach its upper bound. If your relationship with others all reach the upper bound, the game ends.

It's obvious that the game will end at a fixed day regardless your everyday choices. Please calculate how many kinds of ways to end the game. Two ways are said to be different if and only if there exists one day you select the different friend in the two ways.

As the answer may be very large, you should output the answer mod 1000000007

Input Format

The input file contains several test cases, each of them as described below.

  • The first line of the input contains one integers N(1≤N≤1000000), giving the number of friends you have.
  • The second line contains N integers. The i^{th}ith integer represents (1≤Ui​≤1000000), which means the upper bound with ith friend. It's guarantee that the sum of Ui​ is no more than 1000000.

There are no more than 10 test cases.

Output Format

One line per case, an integer indicates the answer mod 1000000007.

样例输入

3
1 1 1
3
1 2 3

样例输出

6
60

 

解题思路:

首先,根据题意不难推得计算公式,即和的阶乘除以阶乘的积。

直接计算一定超时。尝试过取对数计算,但数据量太大,同样超时。

转换思路,注意到(a+b+c+d)! / a! b! c! d! = C(a+b+c+d , a) (b+c+d)! / b! c! d! = C(a+b+c+d , a) C(b+c+d , b) (c+d)! / c! d! = ... =C(a+b+c+d , a) C(b+c+d , b) C(c+d , c) C(d , d)

原式可以写成组合数乘积的形式。

用Lucas定理分别计算各组合数,最后快速积取模即可求解。Lucas计算组合数通过费马小定理求逆元实现。

 

AC代码:

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

typedef long long ll;
const ll mod=1000000007;

ll Jc[1000002];

void calJc()    //求maxn以内的数的阶乘
{
    Jc[0] = Jc[1] = 1;
    for(ll i = 2; i < 1000002; i++)
        Jc[i] = Jc[i - 1] * i % mod;
}

ll pow(ll a, ll n, ll p)    //快速幂 a^n % p
{
    ll ans = 1;
    while(n)
    {
        if(n & 1) ans = ans * a % p;
        a = a * a % p;
        n >>= 1;
    }
    return ans;
}

ll niYuan(ll a, ll b)   //费马小定理求逆元
{
    return pow(a, b - 2, b);
}

ll C(ll a, ll b)    //计算C(a, b)
{
    return Jc[a] * niYuan(Jc[b], mod) % mod
        * niYuan(Jc[a - b], mod) % mod;
}

ll ModMul(ll a,ll b,ll n)//快速积取模 a*b%n
{
    ll ans=0;
    while(b)
    {
        if(b&1)
            ans=(ans+a)%n;
        a=(a+a)%n;
        b>>=1;
    }
    return ans;
}

ll Lucas(ll a, ll b)
{
    if(a < mod && b < mod)
        return C(a, b);
    return
        C(a % mod, b % mod) * Lucas(a / mod, b / mod);
}

int main()
{
	int n;
	calJc();
	while(~scanf("%d",&n))
    {
        ll number;
        ll i,sum=0,ans=1;
        for(i=0;i<n;i++)
        {
            scanf("%lld",&number);
            sum+=number;
            ans=ModMul(ans,Lucas(sum,number),mod);
        }
        printf("%lld\n",ans);
    }
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ACM-ICPC(国际大学生程序设计竞赛)是一项面向大学生的计算机编程竞赛,涉及算法和数据结构等领域。在比赛中,选手需要解决一系列编程问题,使用合适的算法和数据结构来实现正确和高效的解决方案。 对于整理ACM-ICPC模板,以下是一些建议: 1. 了解比赛要求:首先,你需要了解ACM-ICPC比赛的具体要求和规则。这包括了解比赛所涉及的算法和数据结构,以及题目的类型和难度等。 2. 收集资料:收集与ACM-ICPC相关的资料,包括经典算法和数据结构的实现代码、常见问题的解题思路等。可以参考教材、博客、论文等资源。 3. 整理模板:将收集到的资料整理成模板。可以按照算法和数据结构的分类进行整理,例如排序算法、图算法、字符串算法等。对每个模板,添加必要的注释和示例代码,以便理解和使用。 4. 测试代码:对每个模板编写测试代码,确保它们的正确性和可靠性。可以使用已知的测试用例或自行设计测试用例。 5. 更新与扩充:定期更新和扩充模板,以适应ACM-ICPC比赛中新出现的算法和数据结构。同时,根据自己的经验和理解,对模板进行优化和改进。 6. 练习和复习:在比赛之前,利用整理好的模板进行练习和复习。尝试解决一些经典问题,使用模板中的算法和数据结构进行实现,并进行优化。 希望这些建议对你整理ACM-ICPC模板有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值