[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. 【开发笔记】- 将MySQL数据库表中自增ID从0开始

    命令: 用于清空某表的数据 且让自增的id重新从0开始 truncate table 你的表名

  2. 编写可维护的JavaScript-随笔(六)

    避免空比较 If(item !== null){ item.sort(); Item.forEach(function(item){ //执行代码 } } } 以上判断中item期待的是数组类型的,但 ...

  3. 47、安装node-sass后运行报错

    vue安装node-sass编译报错安装node-scss报错安装node-scss报错在搭建vue脚手架 或者是在vue项目中,想使用sass的功能, npm install node-sass - ...

  4. 08-Vuex

    Vuex 一.简介 ① 是什么:是一个状态管理工具,存放项目组件中的公共数据 二.使用语法 ① 语法 -1. 创建 Vuex 实例 const store = new Vuex.Store({ sta ...

  5. 使用SAP CRM中间件XIF(External Interface)一步步创建服务订单

    tcode WE19, choose an existing IDOC in the system: Just change the existing IDOC Service Order ID to ...

  6. java中的强引用、软引用、弱引用、虚引用

    1.强引用(Strong Reference):指程序代码中普遍存在的,类似“Object obj = new Object()”这类的引用,只要对象存在强引用关联,JVM必定不会回收这个对象: 2. ...

  7. CR TubeGet 0.9.2.7,YouTube&全网视频终极下载

    数十次迭代,终于功能完善,在youtube-dl原生支持基础之上,自写解析器脚本,实现对其它主流网站视频下载支持. 加入对视频播放列表.缩略图.字幕下载支持,甚至于自定义列表设计.加密视频下载. 支持 ...

  8. [2019.05.09]Linux 学习笔记(3)

    最近的心得: CLI真好用,GUI就是渣渣 1. Bash 里面的命令是可以起别名的,起一个别名的方法是 alias [Alias]=[command] command可以是任意长的别名,比如 ali ...

  9. 如何为scratch3.0创建一个独立的页面或窗体

    很多人都利用GIT上的scratch3.0做开发,但是苦于有些定制需要个性化开发但是不知道如何动手.本篇文章来做好普及工作吧. 首先需要完成事项如下: 1.需要进行modal定义 2.新增窗口的UI界 ...

  10. php组合设计模式(composite pattern)

    过十点. <?php /* The composite pattern is about treating the hierarchy of objects as a single object ...