题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5719
题意:一个数列为1~N的排列,给定mn[1...n]和mx[1...n],问有符合的排列数为多少?如果不存在,输出0;
思路:
有解的几种条件:
1. mn , mx 变化单调;
2. mn,mx 不能同时变化;
3. 一个位置可选的个数>0;
当解存在时,递推出每次可选择的个数,num += mx[i] - mx[i-1] + mn[i-1] - mn[i] - 1; 即可;
坑:开始想成了求解连续可选区间长度,和连续可选区间的 可选数;然后使用组合数和全排列...并且貌似还是错的;还是及时计数比较好;
及时计数的特点: 在线求出对结果的贡献,并且可以不管其他的状态对当前处理的状态(点)的影响;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
#define A first
#define B second
#define MK make_pair
#define esp 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define clear0 (0xFFFFFFFE) typedef pair<int,int> PII;
const int mod = 998244353;
typedef long long ll;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>9) out(a/10);
putchar(a%10+'0');
}
inline ll gcd(ll a,ll b){ return b == 0? a: gcd(b,a%b); }
const int maxn = 100007;
int a[maxn], b[maxn];
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T, kase = 1;
scanf("%d",&T);
while(T--){
int n, m, flag = 1;
read1(n);
rep1(i,1,n) read1(a[i]);
rep1(i,1,n) read1(b[i]);
if(a[1] != b[1]) flag = 0;
ll ans = 1,num = 0;
for(int i = 2;i <= n && flag; i++){
if(a[i] > a[i-1] || b[i] < b[i-1]) flag = 0;
if(a[i] != a[i-1] && b[i] != b[i-1]) flag = 0;
if(num < 0) flag = 0;
if(a[i] == a[i-1] && b[i] == b[i-1])
ans = (ans * num)% mod;
num += b[i] - b[i-1] + a[i-1] - a[i] - 1;
}
if(!flag) puts("0");
else printf("%lld\n", ans);
}
return 0;
}