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代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
const int mod=1e9+;
ll fac[maxn],facinv[maxn];
ll n,a[maxn]; ll qpow(ll a,ll b,ll p) //快速幂取模
{
ll res=;
while(b)
{
if(b&) res=res*a%p;
b>>=;
a=a*a%p;
}
return res;
} void init() //求出阶乘与逆元
{
fac[]=fac[]=facinv[]=facinv[]=;
for(int i=;i<maxn;i++)
{
fac[i]=fac[i-]*i%mod;
facinv[i]=facinv[i-]*qpow(i,mod-,mod)%mod;
}
} int main()
{
init();
while(cin>>n)
{
ll sum=;
ll ans=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
ans=ans*facinv[a[i]]%mod;
}
ans=ans*fac[sum]%mod;
printf("%d\n",ans);
}
return ;
}
求逆元参考博客:https://blog.csdn.net/baidu_35643793/article/details/75268911
The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元的更多相关文章
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set
Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest I. T-shirt
JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-shirt to ...
- 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 ...
- C.0689-The 2019 ICPC China Shaanxi Provincial Programming Contest
We call a string as a 0689-string if this string only consists of digits '0', '6', '8' and '9'. Give ...
- B.Grid with Arrows-The 2019 ICPC China Shaanxi Provincial Programming Contest
BaoBao has just found a grid with $n$ rows and $m$ columns in his left pocket, where the cell in the ...
- ACM ICPC, Damascus University Collegiate Programming Contest(2018) Solution
A:Martadella Stikes Again 水. #include <bits/stdc++.h> using namespace std; #define ll long lon ...
- 计蒜客 39272.Tree-树链剖分(点权)+带修改区间异或和 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest E.) 2019ICPC西安邀请赛现场赛重现赛
Tree Ming and Hong are playing a simple game called nim game. They have nn piles of stones numbered ...
- 计蒜客 39280.Travel-二分+最短路dijkstra-二分过程中保存结果,因为二分完最后的不一定是结果 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest M.) 2019ICPC西安邀请赛现场赛重现赛
Travel There are nn planets in the MOT galaxy, and each planet has a unique number from 1 \sim n1∼n. ...
- 计蒜客 39279.Swap-打表找规律 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest L.) 2019ICPC西安邀请赛现场赛重现赛
Swap There is a sequence of numbers of length nn, and each number in the sequence is different. Ther ...
随机推荐
- 汇编 OD 标志位 置位相关指令
知识点: l 标志位 置位相关指令 l 标志寄存器PSW 标志寄存器PSW(程序状态字寄存器PSW) 标志寄存器PSW是一个16为的寄存器.它反映了CPU运算的状态特征并且存放某些控制标志. ...
- Centos7 -- glibc 升级失败、意外删除、故意删除后的处理方法
第一部分:测试(如果不是想测试效果,可以直接跳到第三部分) 鉴于不久前 glibc-2.29 升级失败导致一系列的工具无法正常使用,‘’ 本着研究精神的我决定删除 glibc及其库文件 ,测试影响范围 ...
- C#_根据银行卡卡号判断银行名称
/// <summary> /// 银行信息 /// </summary> public class BankInfo { #region 数组形式存储银行BIN号 /// & ...
- webpack笔记
打包 img src src 必须以 点(.) 开始,才能被打包. 如: ./img/logo.png ../img/logo.png 使用 process a.js window.Base_Url ...
- linux下向一个文件中的某行插入数据的做法
sed -i 'ni\x' test.file 表示向test.file文件里的第n行的前面添加x内容sed -i 'na\x' test.file 表示向test.file ...
- Python-集合-17
''' 集合:可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复. {} ''' set1 = set({1,2,3}) # set2 = {1,2,3,[2,3],{'name':'a ...
- 这是一个数学题牛客训练赛E
题目描述 https://www.nowcoder.net/acm/contest/78/E 已知有一个n+1个数的数列,对于给定的A0和An ,当i满足当1<=i<=n-1时有 ...
- 《linux内核设计与实现》第四章
调度程序负责决定哪个进程投入运行,何时运行以及运行多长时间.只有通过调度程序合理调度,系统资源才能最大限度发挥作用,多进程才会有并发执行的效果. 最大限度地利用处理器时间的原则是,只要有可以执行的进程 ...
- Linux内核分析作业第七周
一. 预处理.编译.链接 gcc hello.c -o hello. gcc编译源代码生成最终可执行的二进制程序,GCC后台隐含执行了四个阶段步骤. 预处理 → 编译 → 汇编 → 链接 预处理:编译 ...
- K 班前7次作业成绩汇总
K 班前7次作业成绩汇总 得分榜 千帆竞发 详细 短学号 名 1 2 3 4 5 6 7 TOTAL 505 基智 4.55 1 -2 0 0 -10 4.37 -2.08 414 圳源 5.43 2 ...