[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. Git 分支的一些特殊的使用方式:Bug分支/feature分支/储存现场/

    参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/900388704535136 一般都与dev分支进行合并 Bug分支 Bug分支也是一个分 ...

  2. 使用node+vue实现简单的WebSocket聊天功能

    最近学习了一下websocket的即时通信,感觉非常的强大,这里我用node启动了一个服务进行websocket链接,然后再vue的view里面进行了链接,进行通信,废话不多说,直接上代码吧, 首先, ...

  3. js 递归遍历对象 插入属性 遍历树结构

    // 向 info下面 每一项 插入 isShow test() { const _this = this; _this.info.isShow = false; let iteration = fu ...

  4. jackson 学习资料

    源代码托管地址 https://github.com/FasterXML/jackson https://github.com/FasterXML/jackson-docs http://www.st ...

  5. vue动画理解,进入、离开、列表过度和路由切换。

    vue的动画对于很多初学者,甚至对很多老鸟来说也是很费劲,不容易控制的. 这篇文章讲vue动画的理解.其实没那么难. 动画理解 一个元素从A状态变成B状态,如果这个过程通过某种方式反应在视图上了,那么 ...

  6. Java 面向对象(九)内部类

    一.概述 1.引入 类的成员包括: 1.属性:成员变量2.方法:成员方法3.构造器4.代码块5.内部类:成员内部类 其中 1.2是代表这类事物的特征   其中3.4是初始化类和对象用的   其中5协助 ...

  7. 微服务——SpringCloud(Eureka注册中心搭建)

    IDE:IDEA,说实话,真不怎么喜欢用Eclipse这个IDE,太锻炼人了 配置模式:Grandle 微服务框架:SpringCloud 第一步 创建一个Spring Initializr项目 第二 ...

  8. Linux基础学习之基础命令(1)--2019-11-14

    查看命令路径其他方法: which 命令: which [options] [--] programname [...] -a:显示所有匹配的程序文件,而非第一个: --skip-alias:略过别名 ...

  9. Linux下关于Qt无法调用fcitx的中文输入

    1 本机环境: deepin 15.11 Qt 5.11.3 fcitx 输入法 2 问题描述 Qt Creator 和使用 QT 编译的程序运行时均不能使用deepin系统自带的fcitx输入法,且 ...

  10. 黄金矿工(LeetCode Medium难度)1129题 题解(DFS)

    题目描述: 给定一个二维网络,给定任意起点与终点.每一步可以往4个方向走.要找出黄金最多的一条线路. 很明显的是要“一条路走到黑,一直下去直到某个条件停止”. 运用dfs(深度优先搜索)求解. 因为起 ...