The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

题目来源

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest

35.4%

  • 1000ms
  • 65536K

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 ithi^{th}ith friend has the upper bound UiU_iUi?. 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)(1 \le N \le 1000000)(1≤N≤1000000), giving the number of friends you have.
  • The second line contains NNN integers. The ithi^{th}ith integer represents UiU_iUi?(1≤Ui≤1000000)( 1 \le U_i \le 1000000)(1≤Ui?≤1000000), which means the upper bound with ithi^{th}ith friend. It‘s guarantee that the sum of UiU_iUi? 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题目大意:有n个朋友,开始和每个朋友的关系为0,给出每个朋友的关系上限,每天只能和一个朋友的关系度增加1,求有多少种方式能够和他们的关系都达到上限。解题思路:这道题很明显就是高中数学里面的排列组合题,我们可以先忽略先后顺序,直接把每个关系度看成一个朋友然后去掉是同一朋友的关系度,本质就是多重集合的排列计数问题 令 sum=a(1)+a(2)+。。。+a(n) ,答案就是 sum!/a1!a2!...an! 预处理逆元跟阶乘即可 复杂度:O(nlogn)(预处理)附上AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e6+1;
 5 const int mod=1e9+7;
 6 ll fac[maxn],facinv[maxn];
 7 ll n,a[maxn];
 8
 9 ll qpow(ll a,ll b,ll p)  //快速幂取模
10 {
11     ll res=1;
12     while(b)
13     {
14         if(b&1) res=res*a%p;
15         b>>=1;
16         a=a*a%p;
17     }
18     return res;
19 }
20
21 void init()  //求出阶乘与逆元
22 {
23     fac[0]=fac[1]=facinv[0]=facinv[1]=1;
24     for(int i=2;i<maxn;i++)
25     {
26         fac[i]=fac[i-1]*i%mod;
27         facinv[i]=facinv[i-1]*qpow(i,mod-2,mod)%mod;
28     }
29 }
30
31 int main()
32 {
33     init();
34     while(cin>>n)
35     {
36         ll sum=0;
37         ll ans=1;
38         for(int i=1;i<=n;i++)
39         {
40             scanf("%d",&a[i]);
41             sum+=a[i];
42             ans=ans*facinv[a[i]]%mod;
43         }
44         ans=ans*fac[sum]%mod;
45         printf("%d\n",ans);
46     }
47     return 0;
48 }

原文地址:https://www.cnblogs.com/zjl192628928/p/9383166.html

时间: 2024-10-31 18:20:58

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元的相关文章

The 2018 ACM-ICPC China JiangSu Provincial Programming Contest(第六场)

A Plague Inc Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants to model this game. Let's consider the world has N×Mimes MN×M cities. The world has N rows and M columns. The city in the X row and the Y column has

The 2019 ACM-ICPC China Shannxi Provincial Programming Contest (西安邀请赛重现) J. And And And

链接:https://nanti.jisuanke.com/t/39277 思路: 一开始看着很像树分治,就用树分治写了下,发现因为异或操作的特殊性,我们是可以优化树分治中的容斥操作的,不合理的情况只有当两点在一条链上才存在,那么直接一遍dfs从根节点向下跑途中维护一下前缀和,把所有情况中不合理情况造成的值修正. 这样的话时间复杂度就可以降得非常低了,感觉还可以优化,但是懒得写了 代码耗时:142ms. 实现代码: #include<bits/stdc++.h> using namespace

【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N) and each time a child is inv

sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1)) + A^(f(2)) + A^(f(3)) + ...... + A^(f(n)) ) modular P. 1 <= n <= 10^6 0 <= A, K, a, b <= 10^9 1 <= m, P <= 10^9 本题目的关键在于大幂的分解和..你要这样想,因

[ACM] hdu 3923 Invoker (Poyla计数,快速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In

2018 ACM/ICPC 南京 I题 Magic Potion

题解:最大流板题:增加两个源点,一个汇点.第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1). 每个怪物连终点,边权为1: 参考代码: 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define INF 0x3f3f3f3f 4 const int maxn = 2100; 5 int n,m,k,s,t,u,v,w,num,num1; 6 struct Edge

2018-2019 ICPC Northwestern European Regional Programming Contest (NWERC 2018)

J题队友犯了初始化的错,白给6发,本来能1A的 B: solver:lzh.czq 就是个拓扑排序 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ff first 4 #define ss second 5 #define mp make_pair 6 typedef long long ll; 7 typedef pair<int, int> pii; 8 9 vector<int> g[400

2019-2020 ICPC Southwestern European Regional Programming Contest (SWERC 2019-2020)

J想到了卡特兰数,也想到要按最小值分割数组,丢给队友之后两个人都没做出来,傻了 题目链接:https://codeforces.com/gym/102501 B: solver:czq 1 /* basic header */ 2 #include <bits/stdc++.h> 3 /* define */ 4 #define ll long long 5 #define pb emplace_back 6 #define mp make_pair 7 #define eps 1e-8 8

[ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)

Saving Beans Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a probl