[POI2008]PER-Permutation
[POI2008]PER-Permutation 带重复的康托展开!
根本不需要中国剩余定理就可以A掉!
看完题面你会惊人地发现这好像一个康托展开!(显然是不同的啦)
首先我们来看康托展开这个东西在数组为排列时怎么打
------->度娘
int cantor(int a[],int n){//cantor展开,n表示是n位的全排列,a[]表示全排列的数(用数组表示)
int ans=0,sum=0;
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++)
if(a[j]<a[i])
sum++;
ans+=sum*factorial[n-i];//累积
sum=0;//计数器归零
}
return ans+1;
}
对于每一个数算出贡献,贡献为后面出现的比它小的数(设w个即这一位本来还可以选的数)乘上后面数个数的阶乘
这个w显然可以用树状数组优化
由于后面的数会出现重复,所以我们对于那些重复的数(假设出现了k次),他们会被枚举出\(k!\)种排列,我们要把它除掉
所以这个数\(a[i]\)的贡献为
\]
\(cnt[j]\)表示i~n这一段中每个数 j 出现的个数
对于这个分母,显然不可以高精!我们会自然想到模逆元,但模逆元也是要求互质的!那怎么办?
把\(m\)的因数在答案中出现的提出来,不就互质了吗!(也就是在每次计算时把这些因子除掉,记录提出因子的个数)
由于对于上面的式子 \(\frac{(n-i)!}{\Pi \ {cnt[j]!}}\)
这个东西显然是个整数,(你可以像证明组合数是个整数一样证明它),所以上面的因子个数-下面这些因子的个数显然是自然数,提出来后把剩下的答案乘出来,最后再把那些多出来的因子乘上去就行了
tips:因为模数不是质数,故不能用费马
(你可以看一下我丑陋的code)
typedef long long ll;
#define reg register
#define rep(a,b,c) for(int a=b,a##end=c;a<=a##end;++a)
#define drep(a,b,c) for(int a=b,a##end=c;a>=a##end;--a)
const int N=3e5+10;
int n,m;
void Exgcd(ll a,ll b,ll &x,ll &y){
if(b==0) { x=1,y=0; return ;}
Exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline ll Inv(ll x,ll P){
ll a,b;
Exgcd(x,P,a,b);
return (a%P+P)%P;
}
int a[N];
struct BIT{
ll s[N];
void init(){ memset(s,0,sizeof s); }
void Add(int p,int x){
while(p<N) s[p]+=x,p+=p&-p;
}
ll Que(int p){
int res=0;
while(p) res+=s[p],p^=p&-p;
return res;
}
}Bit;
//树状数组用来求大于i小于a[i]的个数
int c[N],cc[N];
ll fac[N],fcnt,b[N],pq[N];
ll po[50][N];
int cnt[50];
//cnt记录m的每一个因数被提出来的个数
void Count(ll &x,int k){
rep(i,1,fcnt){
int p=fac[i];
while(x%p==0) {
x/=p;
cnt[i]+=k;
}
}
}
ll Get(){
ll res=1;
rep(i,1,fcnt) (res*=po[i][min(cc[i],cnt[i])])%=m;
return res;
}
ll Solve(){
ll ans=1,res=1;
c[a[n]]=1,Bit.Add(a[n],1);
drep(i,n-1,1){
ll t=n-i;
Count(t,1);//计算(n-i)!要多乘上的值,也提出m的因数
(res*=t)%=m;
t=++c[a[i]];//计算分子中变化的值,提出其中m的因数
//这两步保证了求逆元时一定是互质有解的
Count(t,-1);
(res*=Inv(t,m))%=m;
Bit.Add(a[i],1);
t=Get();//计算提出因数的总乘积
(ans+=res*Bit.Que(a[i]-1)%m*t%m)%=m;
//套入计算公式
}
return ans;
}
int main(){
n=rd(),m=rd();
rep(i,1,n) a[i]=rd();
int tmp=m;
for(int i=2;(i*i)<=tmp;i++) if(tmp%i==0){
fac[++fcnt]=i;
po[fcnt][0]=1;
rep(j,1,N-1) po[fcnt][j]=po[fcnt][j-1]*i%m,cc[fcnt]++;
while(tmp%i==0) tmp/=i;
}
if(tmp>1) {
fac[++fcnt]=tmp;
po[fcnt][0]=1;
rep(j,1,N-1) po[fcnt][j]=po[fcnt][j-1]*tmp%m,cc[fcnt]++;
}
//处理出m的因数,预处理次方
printf("%lld\n",Solve());
}
[POI2008]PER-Permutation的更多相关文章
- 洛谷 P3477 [POI2008]PER-Permutation 解题报告
P3477 [POI2008]PER-Permutation 题目描述 Multiset is a mathematical object similar to a set, but each mem ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
- [BZOJ1112][POI2008]砖块Klo
[BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...
随机推荐
- Java自学-异常处理 自定义异常
Java 自定义异常 示例 1 : 创建自定义异常 一个英雄攻击另一个英雄的时候,如果发现另一个英雄已经挂了,就会抛出EnemyHeroIsDeadException 创建一个类EnemyHeroIs ...
- Django---MTV和MVC的了解,Django的模版语言变量和逻辑,常见的模板语言过滤器,自定义过滤器,CSRF了解,Django的母版(继承extends,块block,组件include,静态文件的加载load static),自定义simple_tag和inclusion_tag
Django---MTV和MVC的了解,Django的模版语言变量和逻辑,常见的模板语言过滤器,自定义过滤器,CSRF了解,Django的母版(继承extends,块block,组件include,静 ...
- 基于OpenGL三维软件开发
实验原理: OpenGL在MFC下编程原理---- Windows操作系统对OpenGL的支持 在Windows下用GDI作图必须通过设备上下文(DeviceContext简写DC)调用相应的函数:用 ...
- Windows - CMD窗口UTF8编码乱码问题的解决!
问题描述 用MS-DOC打开 UTF-8 的文件时, 显示乱码问题根源 CMD默认是Windows系统默认编码(GBK), 用GBK格式来解码UTF-8的文件当然会出现乱码.解决方案 ...
- Cheat Engine 模糊数值
打开游戏 玩到换枪为止 换枪 发现子弹数量是有限的200 扫描200 这是初次扫描 开两枪 剩余子弹数量194 再次扫描194 得到地址 尝试得到的这两个地址,经验证,第二个是我们想要的地址 重新开始 ...
- Java 8中的Base64编码和解码
转自:https://juejin.im/post/5c99b2976fb9a070e76376cc Java 8会因为将lambdas,流,新的日期/时间模型和Nashorn JavaScript引 ...
- 7 静态分析Android
静态分析两种方式: 1. 阅读反汇编的Dalvik字节码:使用IDA 分析dex文件或baksmali反编译的smali文件 2. 阅读反汇编的Java源码:使用dex2jar生成jar文件,用jd- ...
- Protobuf协议应用干货
Protobuf应用广泛,尤其作为网络通讯协议最为普遍.本文将详细描述几个让人眼前一亮的protobuf协议设计,对准备应用或已经应用protobuf的开发者会有所启发,甚至可以直接拿过去用. 这里描 ...
- NumPy 之 存储文件和线性代数
import numpy as np File Input and Output NumPy is able to save and load data to and from disk either ...
- Linux系统禁止root账号远程登录
修改配置文件/etc/ssh/sshd_config,去掉PermitRootLogin前的注释,修改值为no,然后重启sshd服务即可 #LoginGraceTime 2m PermitRootLo ...