BZOJ 2142: 礼物
模非素数下的排列组合,简直凶残
调着调着就过了= =
都不知道怎么过的= =
直接上链接http://hi.baidu.com/aekdycoin/blog/item/147620832b567eb40df4d258.html
CODE:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll a[3][50],pri[50],M[50],t[50];
ll ex_gcd(ll a,ll b,ll &x,ll &y) {
if (b==0) {x=1,y=0 ;return a;}
ex_gcd(b,a%b,x,y);
x=x-a/b*y;
swap(x,y);
return 0;
}
ll p;
int l,sum[50];
int fen(int p){
for (int i=2;i<=sqrt(p);i++) {
if (p%i==0) {
pri[++l]=i;
while (p%i==0) {
sum[l]++;
p/=i;
}
}
}
if (p>1) {pri[++l]=p;sum[l]=1;}
return 0;
}
ll power(ll x,ll y,ll mod){
if (y==0) return 1;
ll ans=power(x,y>>1,mod);
ans=ans*ans%mod;
if (y&1) ans=ans*x%mod;
return ans;
}
ll f[100101],cnt;
ll calcfac(ll n,ll p,ll pi){
if (n<pi) return f[n];
// printf("%lld %lld",pi,pri[1]);
ll seg=n/p,rem=n%p;
ll ret=power(f[p-1],seg,p);
ret=ret*f[rem]%p;
cnt+=n/pi;
ret=ret*calcfac(n/pi,p,pi)%p;
return ret;
}
ll china(ll *a,ll *b) {
ll tem,ans=0;
for (int i=2;i<=l;i++) {
ll x,y;
ex_gcd(b[i-1],b[i],x,y);
b[i]=b[i]*b[i-1];
a[i]=((x*(a[i]-a[i-1])*b[i-1]+a[i-1])%b[i]+b[i])%b[i];
}
return a[l];
}
ll b[51];
ll c(int n,int m){
for (int i=1;i<=l;i++) {
ll p=1;
for (int j=1;j<=sum[i];j++) p*=pri[i];
f[0]=1;
for (int j=1;j<p;j++) {
f[j]=f[j-1];
if (j%pri[i]==0) continue;
f[j]=(f[j]*j)%p;
}
cnt=0;ll ans,tem;
a[1][i]=calcfac(n,p,pri[i]);
tem=cnt;cnt=0;
a[2][i]=calcfac(m,p,pri[i]);
printf("%lld\n",pri[1]);
a[3][i]=calcfac(n-m,p,pri[i]);
cnt=tem-cnt;
a[2][i]=a[3][i]*a[2][i]%p;
ex_gcd(a[2][i],p,ans,tem);
ans=(ans%p+p)%p;
a[1][i]=a[1][i]*ans%p*power(pri[i],cnt,p)%p;
b[i]=p;
}
return (china(a[1],b));
}
ll te[8];
int main(){
ll n,m;
ll tmp=0;
scanf("%lld %lld %lld",&p,&n,&m);
for (int i=1;i<=m;i++) {
scanf("%lld",&te[i]);
tmp+=te[i];
}
if (tmp>n) {printf("Impossible");return 0;}
fen(p);
for (int i=1;i<=l;i++) printf("%lld %lld\n",pri[i],sum[i]);
ll ans=1;
for (int i=1;i<=m;i++) {
ans=ans*c(n,te[i])%p;
n-=te[i];
// if (ans==0) {printf("%d\n",0);return 0;}
printf("\n\n");
}
printf("%lld",ans);
return 0;
}
BZOJ 2142: 礼物的更多相关文章
- BZOJ 2142: 礼物 [Lucas定理]
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1294 Solved: 534[Submit][Status][Discuss] ...
- BZOJ 2142 礼物 组合数学 CRT 中国剩余定理
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1450 Solved: 593[Submit][Status][Discuss] ...
- 【刷题】BZOJ 2142 礼物
Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店 ...
- bzoj 2142 礼物——扩展lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 没给P的范围,但说 pi ^ ci<=1e5,一看就是扩展lucas. 学习材料 ...
- bzoj 2142: 礼物【中国剩余定理+组合数学】
参考:http://blog.csdn.net/wzq_qwq/article/details/46709471 首先推组合数,设sum为每个人礼物数的和,那么答案为 \[ ( C_{n}^{sum} ...
- BZOJ.2142.礼物(扩展Lucas)
题目链接 答案就是C(n,m1) * C(n-m1,m2) * C(n-m1-m2,m3)...(mod p) 使用扩展Lucas求解. 一个很简单的优化就是把pi,pi^ki次方存下来,因为每次分解 ...
- BZOJ - 2142 礼物 (扩展Lucas定理)
扩展Lucas定理模板题(貌似这玩意也只能出模板题了吧~~本菜鸡见识鄙薄,有待指正) 原理: https://blog.csdn.net/hqddm1253679098/article/details ...
- BZOJ 2142 礼物 数论
这道题是求组合数终极版. C(n,m) mod P n>=1e9 m>=1e9 P>=1e9且为合数且piqi<=1e5 拓展lucas定理. 实际上就是一点数论小知识的应用. ...
- bzoj 3055礼物运送 floyed + 状压DP
bzoj 3055: 礼物运送 floyed first 设f[i][S]表示取到了S集合中的所有点(不一定是经过的所有点),最后停在了i的最优值. 初始就f[i][{i}] = dis[1][i] ...
随机推荐
- sysctl.conf和limit.conf备忘待查
#################################limits.conf设置################################### #修改最大进程和最大文件打开数限制v ...
- 使用React Native来撰写跨平台的App
React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...
- 单纯觉得是篇好文——跨终端Web之Hybrid App
[reference]http://www.infoq.com/cn/articles/hybrid-app#theCommentsSection 编者按:InfoQ开设新栏目“品味书香”,精选技术书 ...
- Leetcode 181. Employees Earning More Than Their Managers
The Employee table holds all employees including their managers. Every employee has an Id, and there ...
- constrain to margins
如果你点了constrain to margins,左右会有8个点的空挡,而是从8个点后开始计算约束,而没有点时,已屏幕的0点开始计算.
- 字符集 ISO-8859-1(3)
详细见 http://www.w3school.com.cn/tags/html_ref_urlencode.html
- delphi 获取硬盘序列号、cpu号、bios号、网卡号
delphi 获取硬盘 序列号 function GetIdeNum: String; type TSrbIoControl = packed record HeaderLength : ULONG; ...
- origin添加error bar
增加一列,然后set as Y Error,然后全部选中显示就行了.
- js模块化开发——模块的写法
随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个团队分工协作.进度管理.单元测试等等......开发者 ...
- jQuery 动画的执行
jQuery 动画的执行 <%@ page language="java" import="java.util.*" pageEncoding=" ...