Main Topic
Ask how many of the 1~n in the arrangement, so that abs (the value of the first position −i)!=k a B s (the value of the first position −i)! = k ABS (value I position-i)!=k thinking of solving problems
Consider tolerance, ans=∑ni=0 (−1) ig[i] (n−i)! (G[i] indicates that at least I position is not a valid number of scenarios) a n s =∑i = 0 N (−1) i g [i] (n−i)! (g [I] indicates that at least I position is not a valid number of scenarios) Ans=\sum_{i=0}^{n} ( -1) ^ig[i] (n-i)! (G[i] indicates that at least I position is not a valid number of scenarios)
Consider how to ask for G[i]
Place each position and each value as a point, there are 2n points, if I position can not fill J, position I to value J edge.
In this way, a binary graph is obtained, and the problem becomes the number of options to select the I side.
Each strand of the dichotomy is pulled out and together, the 2n points are formed in a row, and some adjacent points have edges.
Set F[I][J][0/1] f [i] [j] [0/1] F[I][J][0/1], that is, to the first point, the J-Edge is selected, the point and the edge of the last point whether there is a choice (if there is no edge of 0) of the scheme number.
So g[i]=f[2n][i]][0]+f[2n][i][1] g [i] = f [2 n] [i]] [0] + F [2 n] [i] [1] g[i]=f[2n][i]][0]+f[2n][i][1]
#include <cmath> #include <iostream> #include <cstdio> #include < cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <map> #include
<bitset> #include <set> const int inf=2147483647;
const int mo=924844033;
const int n=4005;
using namespace Std;
int N,m,tot;
BOOL Lk[n]; Long