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

本文探讨了在Persona5游戏中,玩家如何与N位朋友建立关系的算法问题。通过计算不同方式达到所有关系上限的总数,利用组合数学原理,如Lucas定理和费马小定理,解决了一个复杂的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值