[Problem] Solutions reverse order [51nod1020]

[Problem] Solutions reverse order [51nod1020]

Portal: reverse order \ ([51nod1020] \)

Description [title]

Co \ (T \) set of test points, each set is given \ (2 \) integers \ (n-\) and \ (K \) , in \ ([1, n] \ ) co \ (n-\) full array of numbers, the number of reverse \ (K \) number of permutations of species, the answer to \ (1e9 + 7 \) modulo.

[Sample]

样例输入:
1
4 3

样例输出:
6

【data range】

\(100\%\) \(1 \leqslant T \leqslant 10000,\) \(1 \leqslant N \leqslant 1000,\) \(1 \leqslant K \leqslant 20000\)

【analysis】

With \ (dp [i] [j ] \) represents \ ([1, i] \ ) are arranged in reverse order number \ (J \) number of permutations thereof, for the convenience of the enumeration, with \ (ss [i ] \) represents the length of \ (I \) the total number of aligned \ ((SS [I] = min \ {K, \ FRAC * I {(. 1-I)} {2} \} \) .

For digital \ (I \) , in one \ ([1, i-1 ] \) arranged in it \ (I \) positions may be added, added under (I \) \ reverse after the newly formed log \ (K \ in [0,. 1-I] \) .
Then \ (DP \) equation came out: \ (DP [I] [J] + = DP [I-. 1] [JK] \) , where \ (0 \ leqslant j \ leqslant ss [i], \) \ (0 \ leqslant JK \ leqslant SS [I-. 1] \) , so \ (max \ {0, j -ss [i-1] \} \ leqslant k \ leqslant min \ {j, i-1 \} \) .
That \ (dp [i] [j ] = \ sum_ {k = max \ {0, j-ss [i-1] \}} ^ {min \ {j, i-1 \}} dp [i-1 ] [JK] \) .

Time complexity is too high, and optimized to use the prefix bit, \ (DP [I] [J] = S [J-max (0, J-SS [-I. 1])] - S [J-min (I- . 1, J) -1] \) , where \ (S [I] = \ sum_ ^ {J} = {0} DP I [. 1-I] [J] \) .

However, the prefix and the lower boundary may be taken into the \ (0 \) , the Save \ (1 \) then became \ (- 1 \) , \ (S [-1] \) does not exist, so every time write \ (S [x] \) must be changed \ (S [x + 1] \)

But also the first pre-answer directly when asked \ (O (1) \) queries.

Time complexity is \ (O (NK) \) .

【Code】

#include<algorithm>
#include<cstring>
#include<cstdio>
#define LL long long
#define Re register int
using namespace std;
const int N=1003,M=2e4+3,P=1e9+7;
int T,n,K,S[M],ss[N],dp[N][M];
inline void in(Re &x){
    int f=0;x=0;char c=getchar();
    while(c<'0'||c>'9')f|=c=='-',c=getchar();
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
    x=f?-x:x;
}
int main(){
    n=1000,K=20000;
    for(Re i=2;i<=n;++i){
        ss[i]=i*(i-1)>>1;
        if(ss[i]>K){for(Re j=i;j<=n;++j)ss[j]=K;break;}
    }
    for(Re i=1;i<=n;++i)dp[i][0]=1;
    for(Re j=0;j<=ss[2];++j)(S[j+1]=S[j-1+1]+dp[1][j])%=P;
    for(Re i=2;i<=n;++i){
        for(Re j=1;j<=ss[i];++j)(dp[i][j]=(S[j-max(0,j-ss[i-1])+1]-S[j-min(i-1,j)-1+1]+P)%P)%=P;
        for(Re j=0;j<=ss[i+1];++j)(S[j+1]=S[j-1+1]+dp[i][j])%=P;
    }
    in(T);while(T--)in(n),in(K),printf("%d\n",dp[n][K]);
}

Guess you like

Origin www.cnblogs.com/Xing-Ling/p/11347772.html