icpc 江苏 D Persona5 组合数学 大数阶乘(分段阶乘) 大数阶乘模板
Persona5 is a famous video game.
In the game, you are going to build relationship with your friends.
You have NN friends and each friends have his upper bound of relationship with you. Let's consider the i^{th}ith friend has the upper bound U_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 10000000071000000007
Input Format
The input file contains several test cases, each of them as described below.
- The first line of the input contains one integers NN (1 \le N \le 1000000)(1≤N≤1000000), giving the number of friends you have.
- The second line contains NN integers. The i^{th}ith integer represents U_iUi ( 1 \le U_i \le 1000000)(1≤Ui≤1000000), which means the upper bound with i^{th}ith friend. It's guarantee that the sum of U_iUi is no more than 10000001000000.
There are no more than 1010 test cases.
Output Format
One line per case, an integer indicates the answer mod 10000000071000000007.
样例输入
3
1 1 1
3
1 2 3
样例输出
6
60
题目来源
The 2018 ACM-ICPC China JiangSu Provincial Programming Contest
题意:你有n个朋友,开始你们的默契为0,每天都可以增加一,当你和你的朋友的默契达到每一个人的最高默契值(题目中给出的n个数代表n个朋友的默契值)结束,问你有多少种方式达到最高默契
这个题目可以抽象成我有a1,a2,...,an个球,将这些球放进a1+a2+...+an个箱子里,看有多少种方法?
接下来就是一个排列组合了,很明显我最开始放a1的时候有C(a1,n)种方法,放a2时有C(a2,n-a1)种方法,放an时有C(an,an)种方法,放最后一个时只剩下an个箱子了
所以我的所有方案数是C(a1,n)* C(a2,n-a1)* C(an,an)
考虑到C(a1,n)= (n*(n-1)*...*(n-a1+1))/a1!, C(a2,n-a1)= ((n-a1)*(n-a1-1)*...*(n-a1-a2+1))/a2! ...
所以C(a1,n)* C(a2,n-a1)* C(an,an)= (a1+a2+...+an)!/(a1!*a2!*...*an!)
开始写题目的时候没看到所有的数的和不超过10^6这句话,于是用了分段求阶乘的方法求了大数的阶乘(以为所有数加在一起总和超过了10^12)
这里顺便提下大数的阶乘计算方法把,也是一种应该掌握的计算方法
分块打表,每10000000打一个表,算出阶乘对mod取模的结果
打表程序
#include<iostream>
#include<cstdio>
#define lon long long
using namespace std;
const int maxn=110;
lon n,p,a[maxn];
int main()
{
freopen("1.out","w",stdout);lon ans=1;
for(lon i=0;i<=1000000007;i+=10000000)
{
for(lon j=i+1;j<=i+10000000;j++)
ans=(ans*j)%1000000007;
cout<<ans<<",";
}
return 0;
}
然后判断一下数位于哪个区间,在哪个一路乘就行
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e6;
const int mod = 1e9 + 7;
typedef long long ll;
ll a[maxn+10], b[maxn+10];
ll num[110]={1,682498929,491101308,76479948,723816384,67347853,27368307,
625544428,199888908,888050723,927880474,281863274,661224977,623534362,
970055531,261384175,195888993,66404266,547665832,109838563,933245637,
724691727,368925948,268838846,136026497,112390913,135498044,217544623,
419363534,500780548,668123525,128487469,30977140,522049725,309058615,
386027524,189239124,148528617,940567523,917084264,429277690,996164327,
358655417,568392357,780072518,462639908,275105629,909210595,99199382,
703397904,733333339,97830135,608823837,256141983,141827977,696628828,
637939935,811575797,848924691,131772368,724464507,272814771,326159309,
456152084,903466878,92255682,769795511,373745190,606241871,825871994,
957939114,435887178,852304035,663307737,375297772,217598709,624148346,
671734977,624500515,748510389,203191898,423951674,629786193,672850561,
814362881,823845496,116667533,256473217,627655552,245795606,586445753,
172114298,193781724,778983779,83868974,315103615,965785236,492741665,
377329025,847549272,698611116};
ll qow( ll x, ll y ) {
ll ans = 1;
while( y ) {
if( y&1 ) {
ans = (ans*x)%mod;
}
x = x*x%mod;
y /= 2;
}
return ans;
}
int main() {
std::ios::sync_with_stdio(false);
ll sum = 1;
for( ll i = 1; i <= maxn; i ++ ) {
sum = sum*i%mod;
a[i] = sum;
}
ll n;
while( cin >> n ) {
ll ans = 0;
for( ll i = 0; i < n; i ++ ) {
cin >> b[i];
//debug(a[b[i]]);
ans += b[i];
}
if( ans >= mod ) {
cout << 0 << endl;
continue;
}
ll now = ans/10000000, t = ans;
ans = num[now];
//debug(ans);
for( ll i = now*10000000+1; i <= t; i ++ )
ans = ans*i%mod;
//ans = num[ans];
//debug(ans);
for( ll i = 0; i < n; i ++ ) {
//ans = ans/a[b[i]];
ans = (ans*qow(a[b[i]],mod-2))%mod;
}
cout << ans << endl;
}
return 0;
}
icpc 江苏 D Persona5 组合数学 大数阶乘(分段阶乘) 大数阶乘模板的更多相关文章
- 数论-质数 poj2689,阶乘分解,求阶乘的尾零hdu1124, 求尾零为x的最小阶乘
/* 要求出[1,R]之间的质数会超时,但是要判断[L,R]之间的数是否是素数却不用筛到R 因为要一个合数n的最大质因子不会超过sqrt(n) 所以只要将[2,sqrt(R)]之间的素数筛出来,再用这 ...
- #035 大数阶乘 PTA题目6-10 阶乘计算升级版 (20 分)
实际题目 本题要求实现一个打印非负整数阶乘的函数. 函数接口定义: void Print_Factorial ( const int N ); 其中N是用户传入的参数,其值不超过1000.如果N是非负 ...
- 数组实现int随机数的阶乘(避免大数问题)
面试的一道题目,实现int随机数的阶乘.这道题就是考察你考没考虑大数问题,如何避免它. 我能想到的就是用数组去实现,然后写了一下代码.但是当i的值很大,接近Max_value时的情况还没有考虑到. 直 ...
- 阶乘问题(大数阶乘)简单 n! (一个大数与一个小数相乘的算法 、一个大数与一个小数的除法算法 *【模板】 )
sdut oj 简单n! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个数n(0 <= n <= 150), ...
- 51 Nod 1057 N的阶乘【Java大数乱搞】
1057 N的阶乘 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 输入N求N的阶乘的准确值. Input 输入N(1 <= N <= 10000) Ou ...
- hdu 4704 Sum【组合数学/费马小定理/大数取模】By cellur925
首先,我们珂以抽象出S函数的模型:把n拆成k个正整数,有多少种方案? 答案是C(n-1,k-1). 然后发现我们要求的是一段连续的函数值,仔细思考,并根据组合数的性质,我们珂以发现实际上答案就是在让求 ...
- ICPC Asia Nanning 2017 F. The Chosen One (大数、规律、2的k次幂)
Welcome to the 2017 ACM-ICPC Asia Nanning Regional Contest.Here is a breaking news. Now you have a c ...
- 中科大数分教材:用阶乘倒数和计算e值的误差和e是无理数的证明,用到误差计算
\(e=lim_{n \to \infty}e_{n}(1+\frac{1}{n})^n\\\) \(=\lim_{n \to \infty}(\frac{1}{0!}+\frac{1}{1!}+\f ...
- 求解Catalan数,(大数相乘,大数相除,大数相加)
Catalan数 卡塔兰数是组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰(1814–1894)命名.历史上,清代数学家明安图(1692年-1763年)在其<割圜 ...
随机推荐
- 对Rust所有权、借用及生命周期的理解
Rust的内存管理中涉及所有权.借用与生命周期这三个概念,下面是个人的一点粗浅理解. 一.从内存安全的角度理解Rust中的所有权.借用.生命周期 要理解这三个概念,你首要想的是这么做的出发点是什么-- ...
- 当面对会反制遭破解装置的App该如何顺利提取数据
在检测App的过程之中,总会遇到比较棘手的,以”侦测是否遭破解的装置”为例,便会是个不好处理的状况.当App具备侦测装置是否已遭Root时,一旦发现装置已遭破解,便会停止运行,等于是只准安装及运行在未 ...
- Apache NiFi 核心概念和关键特性
本文来源于官方文档翻译 NiFi 的核心概念 NiFi 最早是美国国家安全局内部使用的工具,用来投递海量的传感器数据.后来由 apache 基金会开源.天生就具备强大的基因.NiFi基本设计理念与 F ...
- Spring Cloud下基于OAUTH2+ZUUL认证授权的实现
Spring Cloud下基于OAUTH2认证授权的实现 在Spring Cloud需要使用OAUTH2来实现多个微服务的统一认证授权,通过向OAUTH服务发送某个类型的grant type进行集中认 ...
- MySQL数据库基本知识(理论总结)
定义:数据库就是一个文件系统,通过sql语句来获取数据 关系型数据库:关系型数据库存放的是实体时间的关系,在数据库层面来看就是存放的是表和表之间的关联关系 常见的关系型数据库 MySQL D ...
- hadoop学习(七)----mapReduce原理以及操作过程
前面我们使用HDFS进行了相关的操作,也了解了HDFS的原理和机制,有了分布式文件系统我们如何去处理文件呢,这就的提到hadoop的第二个组成部分-MapReduce. MapReduce充分借鉴了分 ...
- SprintBoot
简述 推出时间:从Maven仓库的时间看是2016.7.28 目的:摆脱大量的XML配置文件以及复杂的Bean依赖关系,快速.敏捷地开发新一代基于Spring框架的应用程序 思想:约定优于配置(con ...
- (二)对象以及变量的并发访问--synchronized的使用细节,用法
具体的记录synchronized关键的各种使用方式,注意事项.感觉一步一步跟我来都可以看懂滴 大致是按照以下思路进行书写的.黑体字可以理解为结论, 1.synchronized锁的是什么? 2.sy ...
- 解决微信二次分享失败--后面被加上from=singlemessage&isappinstalled=0的解决方案
首次分享成功,点开后再次分享或第三次分享就失败了 1.检查你分享的链接,看是否多了两个参数,微信分享会根据分享的不同,为原始链接拼接: 朋友圈 from=timeline&isappins ...
- 【Isabella Message】 【SPOJ - ISAB】【模拟】【矩阵的旋转】
思路 题目链接 题意:题目中先给了一个N阶矩阵样子的字符,后给了一个mask,然后又给出你应该认识的一些单词,最后是让你输出最终字典序最小的一句话. 思路:根据题目要求模拟即可.这里会用到string ...