3.13模拟赛

· · 个人记录

题面还是不说了。就说说做法吧。

心情复杂。

A

显然是一个偏序集求最长反链,根据dilworth定理,就是求最小链覆盖。

应该没有人只会这个定理但是不会求最小链覆盖的吧。

用DAG上最小路径覆盖的网络流模板就可以了。

但是因为这个DAG有更多优秀的性质,当\forall(x,y),(y,z),必然\exists(x,z)。

可能会有更优秀的做法吧,但是我并不会。

#include<bits/stdc++.h>
typedef long long ll;
const int N = 100020;
const int M = 4000020;
int nex[M],lim[M],to[M],head[N];
int cur[N],d[N],cnt;
const int inf = 1e8;
ll ys[N];
int n,s,t,m;
void add(int x,int y,int z)
{
    nex[++cnt] = head[x];
    lim[cnt] = z;
    to[cnt] = y;
    head[x] = cnt;
}
std::queue<int> q;
bool bfs()
{
    for(int i=1;i<=2*n+2;i++) d[i] = 0;
    while(!q.empty()) q.pop();
    q.push(s);
    d[s] = 1;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        for(int i=head[x];i;i=nex[i])
        if(lim[i]&&!d[to[i]])
        {
            q.push(to[i]);
            d[to[i]] = d[x] + 1;
            if(to[i] == t) 
                return 1;
        }
    }
    return 0;
}
int dinic(int x,int flow)
{
    if(x==t) return flow;
    int rest = flow,k;
    for(int i=head[x];i&&rest;i=nex[i])
    {
        if(lim[i] && d[to[i]] == d[x]+1)
        {
            k = dinic(to[i],std::min(rest,lim[i]));
            if(!k)d[to[i]]=0;
            rest -= k;
            lim[i] -= k;
            lim[i^1] += k;
        }
    }
    return flow - rest;
}
int main()
{
    ll x;
    scanf("%lld",&x);
    cnt = 1;
    for(ll i=1;i*i<=x;i++)if(x%i==0)
    {
        ys[++n] = i ;
        if(i*i!=n)ys[++n] = x/i;
    }
    if(x>1)ys[++n] = x;
    s=2*n+1,t=2*n+2;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)if(i!=j)
            if(ys[j]%ys[i]==0)
                add(i,n+j,1),add(n+j,i,0);
    for(int i=n+1;i<=2*n;i++)add(i,t,1),add(t,i,0);
    for(int i=1;i<=n;i++) add(s,i,1),add(i,s,0);
    int ans = 0;
    while(bfs())
      ans+=dinic(s,inf);
    printf("%d",n-ans);
}

B

C

经典莫队维护出现次数。

但是注意要用光速幂来减少一个\log。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<':'<<x<<endl
const int N = 1e5+20;
struct Q{
    int l,r,id,mod;
}q[N];
int ans[N];
int a[N],cnt[N],rec[N],st[N],used[N],tp;
int n,m,len,cm[N],cm1[N];
bool cmp(Q a,Q b)
{
    int a1=(a.l-1)/len+1,b1=(b.l-1)/len+1;
    return (a1==b1)?a.r<b.r:a.l<b.l;
}
void ins(int x)
{
    x=a[x];
    rec[cnt[x]]-=x;
    rec[++cnt[x]]+=x;
    st[++tp]=cnt[x];
}
void del(int x)
{
    x=a[x];
    rec[cnt[x]]-=x;
    rec[--cnt[x]]+=x;
    st[++tp]=cnt[x];
}
inline int ksm(int x,int up,int mod)
{
    return 1ll*cm[x%up]*cm1[x/up]%mod;
}
int get(int mod,int len)
{
    int up = sqrt(len*2)+1;
    cm[0] = cm1[0] = 1;
    for(int i=1;i<=up;i++)cm[i]=cm[i-1]*2%mod;
    for(int i=1;i<=up;i++)cm1[i]=1ll*cm1[i-1]*cm[up]%mod;
    int ret = 0,ntp = 0,tmp = ksm(len,up,mod);
    for(int i=1;i<=tp;i++)
        if(st[i] && rec[st[i]] && !used[st[i]])
            used[st[i]]=1,st[++ntp]=st[i];
    tp=ntp;
    for(int i=1;i<=tp;i++)
    {
        used[st[i]] = 0;
        int x = rec[st[i]]%mod;
        ret = (ret+1ll*x*(tmp-ksm(len-st[i],up,mod)))%mod;
    }
    return (ret+mod)%mod;
}
int main()
{
    scanf("%d%d",&n,&m);
    len = sqrt(n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)
        scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].mod),q[i].id=i;
    sort(q+1,q+m+1,cmp);
    for(int i=1,l=1,r=0;i<=m;i++)
    {
        Q &x = q[i];
        while(x.l<l)l--,ins(l);
        while(x.r>r)r++,ins(r);
        while(x.l>l)del(l),l++;
        while(x.r<r)del(r),r--;
        ans[x.id] = get(x.mod,x.r-x.l+1);
    }
    for(int i=1;i<=m;i++)
        printf("%d\n",ans[i]);
    return 0;
}
/*
5 2
1 2 2 3 4
2 3 333333
2 4 8
*/