思路
首先将所有数反转,然后钦定我们插入值的顺序为从小到大,将当前的数插入到原来相邻的两个数之间。
这样我们可以得到一个美妙的性质,如果当前一组相邻的点对由于绝对值差距 而不合法,那这个点对在之后也不合法,我们就不用管这个点对了。
考虑 DP,设 为插入了 到 ,能够继续插入数的点对集合为 ,转移直接枚举所有点对即可。
注意到还有一个 右边不能是 的限制,因为有效的限制绝对值都不超过 ,可以直接记录每个点和与它相差不超过 的点的合法状况,然后在弹出一对点对的时候判断是否合法。
时间复杂度 。
别急,这样会被卡常。
下面是卡常小技巧:
- 把存储的点对变为当前点的编号和两点编号之差,这样就可以使用 unsigned char 了。
- 清空 DP 数组的时候可以只清空值,不要 clear。
- 把取模吞成减法。
- 多换几种 c++ 版本试试。
Code
#include<bits/stdc++.h>
namespace fast_IO{
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *P1 = ibuf, *P2 = ibuf, *P3 = obuf;
#define getchar() ((P1==P2)and(P2=(P1=ibuf)+fread(ibuf,1,IOSIZE,stdin),P1==P2)?(EOF):(*P1++))
#define putchar(x) ((P3==obuf+IOSIZE)&&(fwrite(obuf,P3-obuf,1,stdout),P3=obuf),*P3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w;}
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true;}
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48);}
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true;}
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true;}
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++);}
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]);}
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true;}
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]);}
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true;}
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...);}
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...);}
struct Fast_IO { ~Fast_IO() { fwrite(obuf, P3 - obuf, 1, stdout);}}io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b){return read(b),io;}
template<typename T> Fast_IO& operator << (Fast_IO &io, T b){return print(b),io;}
#define cout io
#define cin io
}using namespace fast_IO;
using namespace std;
#define pii pair<unsigned char,unsigned char>
#define pb push_back
const int N=1e6+10,mod=1e9+7;
int n,k,limit;
bitset<10>mp[N];
map<basic_string<pii>,unsigned int>dp[2];
inline int add(int a,int b){return ((a+=b)>=mod)?(a-mod):a;}
inline int norm(int a,int b){return ((a-=b)<=0)?(a+mod):a;}
inline void Add(unsigned int&x,unsigned int y){x=(x+y>=mod?x+y-mod:x+y);}
inline void trans(int opt,int now,pair<basic_string<pii>,int> p){
basic_string<pii>vec;
int len=p.first.size();
for(int i=0;i<len;++i){
if(p.first[i].first+1<=limit&&p.first[i].second+1<=limit)
vec.pb(pii(p.first[i].first+1,p.first[i].second+1));
else if(mp[now-1-p.first[i].first][p.first[i].second-p.first[i].first+limit])return;
}
if(vec.empty())return;
len=vec.size()-1;
for(int i=0;i<=len;++i){
basic_string<pii>vec2=vec;
// vec2.erase(&vec2[i]);
// vec2.insert(&vec2[i],pii(vec[i].first,now));
// vec2.insert(&vec2[i+1],pii(now,vec[i].second));
vec2[i]=pii(vec[i].first,0);
vec2.pb(pii(0,vec[i].second));
for(int j=len+1;j>i+1;--j)swap(vec2[j],vec2[j-1]);
Add(dp[opt][vec2],p.second);
}
}
signed main(){
cin>>n>>k>>limit;
// n=1e6,k=0,limit=3;
if(!limit){
cout<<(n==1)<<"\n";
return 0;
}
if(limit==1){
cout<<((n==1)||(n==2&&(!k)))<<"\n";
return 0;
}
if(n==1){
cout<<"1\n";
return 0;
}
int u,v;
while(k--){
cin>>u>>v;
if(abs(u-v)>limit)continue;
u=n-u+1,v=n-v+1;
mp[u][u-v+limit]=1;
}
basic_string<pii>vec;
vec.resize(2);
vec[0]=pii(1,0),vec[1]=pii(0,1);
int opt=0;
dp[opt][vec]=1;
for(int i=3;i<=n;++i){
opt^=1;
if(dp[opt].size()!=16)dp[opt].clear();
else for(auto &p:dp[opt])p.second=0;
for(auto p:dp[!opt])
trans(opt,i,p);
// for(auto p:dp[opt]){
// cout<<"114514\n";
// for(auto qwq:p.first)cout<<qwq.first<<" "<<qwq.second<<"\n";
// cout<<p.second<<"\n";
// }
}
unsigned int ans=0;
for(auto p:dp[opt]){
int len=p.first.size();
for(int i=0;i<len;++i)
if(mp[n-p.first[i].first][p.first[i].second-p.first[i].first+limit])
goto no;
ans=add(ans,p.second);
no:;
}
cout<<ans<<"\n";
cerr<<"\nTime:"<<clock()/1.0/CLOCKS_PER_SEC*1000.0<<" ms\n";
return 0;
}