1129: [POI2008]Per 中国剩余定理

Description
给你一个序列s,你把这个序列的所有不同排列按字典序排列后,求s的排名mod m

题解:

完全不会啊……代码都几乎是照着栋老师打的……这题思维难度不算太大(但我依然没有想出来),就是一位一位固定,算每一位上比当前排列小的有多少个,然后就可以推出式子(具体的式子可以看别人的blog),然后用中国剩余定理合并就行了,但这个东西我只在学扩展lucas的时候用过,已经忘了,于是又粗略地学了一次,要注意当要乘或除的数与当前的模数有公因子的话,就一直除,直到没有公因子,统计现在还需要乘或除多少个 p p <script type="math/tex" id="MathJax-Element-17">p</script>,然后再乘或除。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxn=300010;
const int inf=2147483647;
LL read()
{
    LL x=0,f=1;char ch=getchar();   
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*f;
}
struct lsh{int v,id;}A[Maxn];
bool cmp(lsh a,lsh b){return a.v<b.v;}
int n,a[Maxn];LL mod;
int s[Maxn];
void add(int x,int t){for(;x<=n;x+=(x&-x))s[x]=(s[x]+t)%mod;}
int query(int x){int re=0;for(;x;x-=(x&-x))re=(re+s[x])%mod;return re;}
LL exgcd(LL a,LL b,LL &x,LL &y)
{
    if(!a){x=0,y=1;return b;}
    LL tx,ty,d=exgcd(b%a,a,tx,ty);
    x=ty-(b/a)*tx;
    y=tx;
    return d;
}
LL Pow(LL x,int y,LL MOD)
{
    if(!y)return 1%MOD;
    if(y==1)return x%MOD;
    LL t=Pow(x,y>>1,MOD),re=t*t%MOD;
    if(y&1)re=re*x%MOD;return re;
}
int p1[Maxn],p2[Maxn],lp=0;
int p,pm,inv[Maxn];
void pre()
{
    memset(inv,0,sizeof(inv));
    inv[1]=1;int phi=pm/p*(p-1);
    for(int i=2;i<=n;i++)if(i%p)inv[i]=Pow(i,phi-1,pm);
}
int tot[Maxn],mx;
LL get()
{
    LL v=1,re=0;int cnt=0;pre();
    for(int i=1;i<n;i++)
    {
        tot[a[i]]++;
        int t=i;
        while(t%p==0)t/=p,cnt++;
        v=v*(LL)(t)%pm;
    }tot[a[n]]++;
    for(int i=1;i<=mx;i++)
    {
        add(i,tot[i]);
        for(int j=2;j<=tot[i];j++)
        {
            int t=j;
            while(t%p==0)t/=p,cnt--;
            v=v*(LL)(inv[t])%pm;
        }
    }
    for(int i=1;i<=n;i++)
    {
        LL t1=query(a[i]-1);
        LL t2;if(cnt<0)t2=Pow(p,-cnt,inf);else t2=Pow(p,cnt,pm);
        LL t3;if(cnt<0)t3=t1/t2*v%pm;else t3=t1*t2%pm*v%pm;
        re=(re+t3)%pm;
        if(i!=n)
        {
            int t;
            t=n-i;while(t%p==0)t/=p,cnt--;
            v=v*inv[t]%pm;
            t=tot[a[i]];while(t%p==0)t/=p,cnt++;
            v=v*t%pm;
        }
        tot[a[i]]--;add(a[i],-1);
    }
    return re;
}
LL Inv(LL a,LL mod)
{
    LL x,y;exgcd(a,mod,x,y);
    return (x%mod+mod)%mod;
}
LL solve()
{
    int t=mod;LL re=0;
    for(int i=2;i*i<=mod;i++)
    if(t%i==0)
    {
        p1[++lp]=i;p2[lp]=0;
        while(t%i==0)t/=i,p2[lp]++;
    }
    if(t!=1)p1[++lp]=t,p2[lp]=1;
    for(int i=1;i<=lp;i++)
    {
        p=p1[i];pm=1;
        for(int j=1;j<=p2[i];j++)pm*=p;
        LL c=get();
        re=(re+Inv(mod/pm,pm)*c%mod*(LL)(mod/pm)%mod)%mod;
    }return (re+1)%mod;
}
int main()
{
    n=read(),mod=read();
    for(int i=1;i<=n;i++)A[i].v=read(),A[i].id=i;
    sort(A+1,A+1+n,cmp);
    a[A[1].id]=1;int cnt=1;
    for(int i=2;i<=n;i++)a[A[i].id]=(cnt+=((A[i].v==A[i-1].v)?0:1));
    mx=cnt;
    printf("%lld",solve());
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值