uoj # 209 [UER # 6] tallies

topic

A problem do UER of up confidence

First, we note that there is at least one so-called correct in \ (x \) and \ (y \) are not equal when very weak, when \ (x <y \) , only possible after the \ (y \) Bit users \ (X \) th pass; when \ (x> y \) , the former may be only \ (X \) users have \ (Y \) th pass. That information can be transformed into some of the information and to limit the prefix and suffix.

Set \ (pre_i \) represents the prefix sequences and, for the first one \ (X \) users have \ (Y \) limitation adopted, we can split into \ (pre_x = Y \) ; For the latter a \ ( Y \) users have \ (X \) th information can be regarded as \ (pre_n NY-pre_} = {X \) , i.e. \ (pre_} = {pre_n NY-X \) .

If we know \ (pre_n \) value, then it is only a prefix and some of the information. So we can directly enumerate \ (pre_n \) values. These in turn limit on prefixes and the entire sequence is divided into a number of sections, each section of the zone and also be limited well, the number of combinations to be used directly for each section of the program worked out just fine, the answer is that every interval the product of the combination.

However, these approaches can not handle \ (x = y \) case, when the \ (x = y \) , when a mean length \ (X \) prefix or suffix are all \ (1 \) . The \ (x \) the greater the restriction will inevitably stronger, so we need only consider the biggest \ (x = y \) , to meet the biggest \ (x = y \) remaining \ (x = y \) They must also have satisfied.

We enumerate the \ (x = y \) restriction prefix or suffix restriction, restriction prefix to split into \ (pre_x = X \) , split into limit suffix \ (NX pre_} = {X \) . But if there is a program both for a full \ (1 \) prefix, but also there is a full \ (1 \) suffix, it will be counted twice. So we put together two restrictions are, and then to lose such a program just fine.

Code

#include<bits/stdc++.h>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
inline int read() {
    char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const int maxn=5e3+5;
const int mod=998244353;
int T,n,m,M;
int fac[maxn],ifac[maxn],inv[maxn];
int a[maxn],b[maxn],c[maxn],d[maxn],t[2],pre[maxn];
inline int C(int n,int m) {
    return m>n?0:1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
inline int solve(int sum) {
    for(re int i=1;i<=t[0];i++) {
        if(pre[a[i]]!=-1&&pre[a[i]]!=c[i]) return 0;
        pre[a[i]]=c[i];
    }
    for(re int i=1;i<=t[1];i++) {
        if(pre[n-b[i]]!=-1&&pre[n-b[i]]!=sum-d[i]) return 0;
        pre[n-b[i]]=sum-d[i]; 
    }
    if(pre[0]!=-1&&pre[0]!=0) return 0;
    pre[0]=0;int l=0,tot=1;
    for(re int i=1;i<=n;i++) {
        if(pre[i]==-1) continue;
        if(pre[i]<pre[l]) return 0;
        tot=1ll*tot*C(i-l,pre[i]-pre[l])%mod;l=i;
    }
    return tot;
}
int main() {
    T=read();fac[0]=ifac[0]=inv[1]=1;
    for(re int i=1;i<maxn;i++) fac[i]=1ll*fac[i-1]*i%mod;
    for(re int i=2;i<maxn;i++) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
    for(re int i=1;i<maxn;i++) ifac[i]=1ll*ifac[i-1]*inv[i]%mod;
    while(T--) {
        n=read(),m=read();int x,y;t[0]=t[1]=M=0;
        for(re int i=1;i<=m;i++) {
            x=read(),y=read();
            if(x<y) b[++t[1]]=y,d[t[1]]=x;
            if(x>y) a[++t[0]]=x,c[t[0]]=y;
            if(x==y) M=max(M,x);
        }
        int ans=0,now=M;
        for(re int i=1;i<=t[0];i++) now=max(now,c[i]);
        for(re int i=1;i<=t[1];i++) now=max(now,d[i]);
        for(re int i=now;i<=n;i++) {
            memset(pre,-1,sizeof(pre));
            pre[n]=i,pre[M]=M;
            ans=(ans+solve(i))%mod;
            if(!M) continue;
            memset(pre,-1,sizeof(pre));
            pre[n]=i,pre[n-M]=i-M;
            ans=(ans+solve(i))%mod;
            memset(pre,-1,sizeof(pre));
            pre[n]=i,pre[M]=M;pre[n-M]=i-M;
            if(M==n-M&&M!=i-M) continue;
            ans=(ans-solve(i)+mod)%mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/11391021.html