[1.8] Diary

1.8

DP

1.CF1091D: Given n, for all n lexicographically arranged in ascending order, asked how many substrings meet a 1-n are arranged.

Thinking: actually see each adjacent to the first two positions arranged in different numbers, it is counted based on this feature.

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mid ((l+r)>>1)
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=1e6+20,P=998244353;
struct TTTT{
    int n;
    LL Inv[M];
    void init(){
        scanf("%d",&n);
        Inv[0]=Inv[1]=1;
        for(int i=2;i<=n;++i)
            Inv[i]=(P-P/i)*Inv[P%i]%P;
    }
    void run(){
        init();
        LL ans=n,sum=1LL*n*(n-2)%P;
        for(int i=2;i<=n;++i)
            ans=(ans+sum*i%P)%P,sum=sum*Inv[n-i]%P*(n-i+1)%P*(n-i-1)%P;
        printf("%lld\n",ans);
    }
}TTT;
int main(){
    TTT.run();
    return 0;
}

Guess you like

Origin www.cnblogs.com/diorvh/p/12189777.html