bzoj 4338: BJOI2015 糖果
4338: BJOI2015 糖果
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: 200 Solved: 93
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 3 3 10
【样例输入2】
2 2 2 10
Sample Output
0
【样例输出2】
6
HINT

#include<bits/stdc++.h>
#define ll long long
#define maxn 100005
using namespace std;
int N,M,K,P,MOD;
int d[15],D[15];
int mo,phi[15];
int ans[15],num;
int jc[maxn],inv[maxn]; inline int add(int x,int y,const int ha){
x+=y;
if(x>=ha) return x-ha;
else return x;
} inline int ksm(int x,int y,const int ha){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} struct node{
int val,tmp;
node operator *(const node &U)const{
return (node){val*(ll)U.val%D[mo],tmp+U.tmp};
}
node operator /(const node &U)const{
return (node){val*(ll)ksm(U.val,phi[mo]-1,D[mo])%D[mo],tmp-U.tmp};
}
}; inline void dvd(){
for(int i=2;i*(ll)i<=P;i++) if(!(P%i)){
d[++num]=i,D[num]=1;
while(!(P%i)) P/=i,D[num]*=i;
phi[num]=D[num]/d[num]*(d[num]-1); if(P==1) break;
} if(P!=1) d[++num]=D[num]=P,phi[num]=P-1;
} inline node getjc(int x){
node now=(node){1,0};
if(x>=d[mo]) now=now*getjc(x/d[mo]),now.tmp+=x/d[mo];
if(x>=D[mo]) now=now*(node){ksm(jc[D[mo]-1],x/D[mo],D[mo]),0};
now=now*(node){jc[x%D[mo]],0}; return now;
} inline node getC(int x,int y){
return getjc(x)/getjc(y)/getjc(x-y);
} inline int getP(int x,int y,const int ha){
int now=x;
for(int i=2;i<=y;i++){
now=add(now,ha-1,ha);
x=x*(ll)now%ha;
}
return x;
} inline void solve(int x){
mo=x,jc[0]=1;
const int ha=D[x];
if(d[x]==D[x]&&d[x]>M){
inv[1]=1;
for(int i=2;i<=M;i++) inv[i]=-inv[ha%i]*(ll)(ha/i)%ha+ha; ans[x]=1;
int now=K+M-1;
for(int i=1;i<=M;i++,now=add(now,ha-1,ha)) ans[x]=ans[x]*(ll)now%ha*(ll)inv[i]%ha; ans[x]=getP(ans[x],N,ha);
}
else{
for(int i=1;i<ha;i++){
jc[i]=jc[i-1];
if(i%d[x]) jc[i]=jc[i]*(ll)i%ha;
} node now=getC(K+M-1,M);
ans[x]=now.val*(ll)ksm(d[x],now.tmp,ha)%ha; ans[x]=getP(ans[x],N,ha);
}
} inline int CRT(){
int an=0;
for(int i=1;i<=num;i++){
mo=i;
an=add(an,(MOD/D[i])*(ll)ksm(MOD/D[i],phi[i]-1,D[i])%MOD*(ll)ans[i]%MOD,MOD);
}
return an;
} int main(){
scanf("%d%d%d%d",&N,&M,&K,&P),MOD=P;
dvd();
for(int i=1;i<=num;i++) solve(i);
printf("%d\n",CRT());
return 0;
}
bzoj 4338: BJOI2015 糖果的更多相关文章
- [BZOJ 1045] [HAOI2008] 糖果传递
题目链接:BZOJ 1045 Attention:数据范围中 n <= 10^5 ,实际数据范围比这要大,将数组开到 10^6 就没有问题了. 我们先来看一下下面的这个问题. 若 n 个人坐成一 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
- BZOJ 4337: BJOI2015 树的同构 树hash
4337: BJOI2015 树的同构 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4337 Description 树是一种很常见的数 ...
- BZOJ 1045: [HAOI2008] 糖果传递 数学
1045: [HAOI2008] 糖果传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1045 Description 有n个小朋友坐 ...
- bzoj 3052: [wc2013]糖果公园 带修改莫队
3052: [wc2013]糖果公园 Time Limit: 250 Sec Memory Limit: 512 MBSubmit: 506 Solved: 189[Submit][Status] ...
- bzoj 1045: [HAOI2008] 糖果传递 贪心
1045: [HAOI2008] 糖果传递 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1812 Solved: 846[Submit][Stat ...
- [BZOJ 3052] [wc2013] 糖果公园 【树上莫队】
题目链接:BZOJ - 3052 题目分析 这道题就是非常经典的树上莫队了,并且是带修改的莫队. 带修改的莫队:将询问按照 左端点所在的块编号为第一关键字,右端点所在的块为第二关键字,位于第几次修改之 ...
- bzoj 2330 [SCOI2011]糖果(差分约束系统)
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3574 Solved: 1077[Submit][Status ...
- BZOJ 2330: [SCOI2011]糖果( 差分约束 )
坑爹...要求最小值要转成最长路来做.... 小于关系要转化一下 , A < B -> A <= B - 1 ------------------------------------ ...
随机推荐
- python 学习分享-socket编程
socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制,取后一种意思. 通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟 ...
- hnust 好友互动标识
问题 A: 好友互动标识 时间限制: 1 Sec 内存限制: 128 MB提交: 897 解决: 122[提交][状态][讨论版] 题目描述 QQ有一个有趣的功能即显示好友互动标识,它 ...
- Set(), Get() 真正的目的
在各种面向对象编程中,都有 Set(), Get() 两种方法. 1 常见理解 1 为了保证安全性 2 为了规范代码 其实这些理解都是对的.具体看我们从哪个角度去理解这个内容. 2 个人理解 2.1 ...
- React01补充
使用yarn安装脚手架 npm i -g yarn npm uninstall -g create-react-app yarn global add create-react-app create- ...
- jquery serialize() 方法
ajax异步提交的时候,会使用该方法. 方法:jQuery ajax - serialize() 方法
- 第六章 系统配置:DHCP和自动配置
系统配置:DHCP和自动配置 写在开头:今天和导师见了个面,抛给我一堆材料以及论文,感觉自己学业更加繁重.有些知识现阶段我可能没办法掌握,但是至少在我需要进一步理解它的时候,要知道在哪个地方能够找到. ...
- VMware 密匙
11.0版本 1F04Z-6D111-7Z029-AV0Q4-3AEH8 亲测可用
- 雅礼集训 Day6 T2 Equation 解题报告
Equation 题目描述 有一棵\(n\)个点的以\(1\)为根的树,以及\(n\)个整数变量\(x_i\).树上\(i\)的父亲是\(f_i\),每条边\((i,f_i)\)有一个权值\(w_i\ ...
- Gcd反应堆 (pgcd)
Gcd反应堆 (pgcd) 题目描述 不知什么时候起,TA突然对gcd产生了浓厚的兴趣,于是他为此编写了个程序,输入分别不大于m,n (1<m,n<=10^7)的两个数,就能得出gcd(m ...
- UVa10025 The ? 1 ? 2 ? ... ? n = k problem 数学思维+规律
UVa10025 ? 1 ? 2 ? ... ? n = k problem The problem Given the following formula, one can set operator ...