[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]\)的贡献为

\[w \cdot \frac{(n-i)!}{\Pi \ {cnt[j]!}}
\]

\(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的更多相关文章

  1. 洛谷 P3477 [POI2008]PER-Permutation 解题报告

    P3477 [POI2008]PER-Permutation 题目描述 Multiset is a mathematical object similar to a set, but each mem ...

  2. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  5. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  7. 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 ...

  8. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  9. 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 ...

  10. [BZOJ1112][POI2008]砖块Klo

    [BZOJ1112][POI2008]砖块Klo 试题描述 N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另 ...

随机推荐

  1. MFC中窗口重绘

    搬家于CSDN 2015-05-14 MFC提供了三个函数用于窗口重绘 InvalidateRect(&Rect) Invalidate() UpdateWindow() 当需要更新或者重绘窗 ...

  2. iOS - 屏幕刷新 ADisplayLink

    什么是CADisplayLink CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个r ...

  3. android黑白屏的问题

    你会很奇怪,为什么有些app启动时,会出现一会儿的黑屏或者白屏才进入Activity的界面显示,但是有些app却不会如QQ手机端,的确这里要做处理一下.这里先了解一下为什么会出现这样的现象,其实很简单 ...

  4. Java abstract关键字 抽象类 抽象方法

    用 abstract 修饰的类是抽象类,它不能生成对象 含有抽象方法的类称为抽象类 抽象方法使用 abstract 修饰 抽象类不能直接实例化,只能由子类实例化 举例 abstract class T ...

  5. Java梗概

    Java平台:J2SE(桌面).J2ME(逐渐被android取代).J2EE(企业级针对web程序) Java是在JVM虚拟机上运行,跨平台本质是在不同平台上运行JVM虚拟机 JRE = JVM+核 ...

  6. ML- 核函数(Kernel) 的 SVM

    Why 核函数 目的是为了解决线性不可分问题. 核心思想是升维. 当样本点在低维空间不能很好地分开的时候, 可以考虑将样本通过某种映射(就是左乘一个矩阵) 到高维空间中, 然后在高维空间就容易求解一个 ...

  7. Mac安装vscode IDE 撸nodejs代码

    1. vscode官网地址:https://code.visualstudio.com   找到mac对应的下载地址,下载后的文件是 zip压缩包,解压后将文件拖到Application目录下即可. ...

  8. windows安装mysql8详解

    MySQL的安装过程还是比较繁琐,为了以后安装节约时间,将其详细安装过程总结如下: 1>下载对应版本 下载地址:https://dev.mysql.com/downloads/mysql/ 百度 ...

  9. Httpd服务入门知识-Httpd服务常见配置案例之Apache的工作做状态status页面

    Httpd服务入门知识-Httpd服务常见配置案例之Apache的工作做状态status页面 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.status功能概述 status页 ...

  10. 1.探索性数据分析(EDA,Exploratory Data Analysis)

    一.数据探索 1.数据读取 遍历文件夹,读取文件夹下各个文件的名字:os.listdir() 方法:用于返回指定的文件夹包含的文件或文件夹的名字的列表.这个列表以字母顺序. 它不包括 '.' 和'.. ...