HDU 5719 Arrange
2016-07-18 19:19
309 查看
Problem Description
Accidentally, Cupid, god of desire has hurt himself with his own dart and fallen in love with Psyche.
This has drawn the fury of his mother, Venus. The goddess then throws before Psyche a great mass of mixed crops.
There are n heaps
of crops in total, numbered from 1 to n.
Psyche needs to arrange them in a certain order, assume crops on the i-th
position is Ai.
She is given some information about the final order of the crops:
1. the minimum value of A1,A2,...,Ai is Bi.
2. the maximum value of A1,A2,...,Ai is Ci.
She wants to know the number of valid permutations. As this number can be large, output it modulo 998244353.
Note that if there is no valid permutation, the answer is 0.
Input
The first line of input contains an integer T (1≤T≤15),
which denotes the number of testcases.
For each test case, the first line of input contains single integer n (1≤n≤105).
The second line contains n integers,
the i-th
integer denotes Bi (1≤Bi≤n).
The third line contains n integers,
the i-th
integer denotes Ci (1≤Ci≤n).
Output
For each testcase, print the number of valid permutations modulo 998244353.
Sample Input
2
3
2 1 1
2 2 3
5
5 4 3 2 1
1 2 3 4 5
Sample Output
1
0
Hint
In the first example, there is only one valid permutation (2,1,3) .
In the second example, it is obvious that there is no valid permutation.
a数组递减,b数组递增,不能同时不同,第一个点必相同,数字不够也会炸,不然就是合法的,乘起来就行了
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const int N = 1e5 + 10;
const int mod = 998244353;
const int INF = 0x7FFFFFFF;
int T, n, a
, b
;
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
int ans = 1, t = 1;
rep(i, 1, n) scanf("%d", &a[i]);
rep(i, 1, n) scanf("%d", &b[i]);
rep(i, 2, n)
{
if (a[i] > a[i - 1]) ans = 0;
if (b[i] < b[i - 1]) ans = 0;
if (a[i] != a[i - 1] && b[i] != b[i - 1]) ans = 0;
}
if (a[1] != b[1]) ans = 0;
rep(i, 2, n)
{
--t;
if (a[i] != a[i - 1]) { t += a[i - 1] - a[i]; continue; }
if (b[i] != b[i - 1]) { t += b[i] - b[i - 1]; continue; }
ans = (int)(1LL * ans * t % mod);
}
printf("%d\n", ans);
}
}
Accidentally, Cupid, god of desire has hurt himself with his own dart and fallen in love with Psyche.
This has drawn the fury of his mother, Venus. The goddess then throws before Psyche a great mass of mixed crops.
There are n heaps
of crops in total, numbered from 1 to n.
Psyche needs to arrange them in a certain order, assume crops on the i-th
position is Ai.
She is given some information about the final order of the crops:
1. the minimum value of A1,A2,...,Ai is Bi.
2. the maximum value of A1,A2,...,Ai is Ci.
She wants to know the number of valid permutations. As this number can be large, output it modulo 998244353.
Note that if there is no valid permutation, the answer is 0.
Input
The first line of input contains an integer T (1≤T≤15),
which denotes the number of testcases.
For each test case, the first line of input contains single integer n (1≤n≤105).
The second line contains n integers,
the i-th
integer denotes Bi (1≤Bi≤n).
The third line contains n integers,
the i-th
integer denotes Ci (1≤Ci≤n).
Output
For each testcase, print the number of valid permutations modulo 998244353.
Sample Input
2
3
2 1 1
2 2 3
5
5 4 3 2 1
1 2 3 4 5
Sample Output
1
0
Hint
In the first example, there is only one valid permutation (2,1,3) .
In the second example, it is obvious that there is no valid permutation.
a数组递减,b数组递增,不能同时不同,第一个点必相同,数字不够也会炸,不然就是合法的,乘起来就行了
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const int N = 1e5 + 10;
const int mod = 998244353;
const int INF = 0x7FFFFFFF;
int T, n, a
, b
;
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
int ans = 1, t = 1;
rep(i, 1, n) scanf("%d", &a[i]);
rep(i, 1, n) scanf("%d", &b[i]);
rep(i, 2, n)
{
if (a[i] > a[i - 1]) ans = 0;
if (b[i] < b[i - 1]) ans = 0;
if (a[i] != a[i - 1] && b[i] != b[i - 1]) ans = 0;
}
if (a[1] != b[1]) ans = 0;
rep(i, 2, n)
{
--t;
if (a[i] != a[i - 1]) { t += a[i - 1] - a[i]; continue; }
if (b[i] != b[i - 1]) { t += b[i] - b[i - 1]; continue; }
ans = (int)(1LL * ans * t % mod);
}
printf("%d\n", ans);
}
}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- HDU 1016 Prime Ring Problem
- HDU 1017 A Mathematical Curiosity