题目链接

Problem Description

Have you ever seen the wave? It's a wonderful view of nature. Little Q is attracted to such wonderful thing, he even likes everything that looks like wave. Formally, he defines a sequence a1,a2,...,an as ''wavel'' if and only if a1a3a5<a6...

Picture from Wikimedia Commons

Now given two sequences a1,a2,...,an and b1,b2,...,bm, Little Q wants to find two sequences f1,f2,...,fk(1≤fi≤n,fi<fi+1) and g1,g2,...,gk(1≤gi≤m,gi<gi+1), where afi=bgi always holds and sequence af1,af2,...,afk is ''wavel''.

Moreover, Little Q is wondering how many such two sequences f and g he can find. Please write a program to help him figure out the answer.

Input

The first line of the input contains an integer T(1≤T≤15), denoting the number of test cases.

In each test case, there are 2 integers n,m(1≤n,m≤2000) in the first line, denoting the length of a and b.

In the next line, there are n integers a1,a2,...,an(1≤ai≤2000), denoting the sequence a.

Then in the next line, there are m integers b1,b2,...,bm(1≤bi≤2000), denoting the sequence b.

Output

For each test case, print a single line containing an integer, denoting the answer. Since the answer may be very large, please print the answer modulo 998244353.

Sample Input

1

3 5

1 5 3

4 1 1 5 3

Sample Output

10

Hint

(1)f=(1),g=(2).

(2)f=(1),g=(3).

(3)f=(2),g=(4).

(4)f=(3),g=(5).

(5)f=(1,2),g=(2,4).

(6)f=(1,2),g=(3,4).

(7)f=(1,3),g=(2,5).

(8)f=(1,3),g=(3,5).

(9)f=(1,2,3),g=(2,4,5).

(10)f=(1,2,3),g=(3,4,5).

题意:

给定数组a和数组b,数组a与数组b中的元素要分别构成“小 大 小 大 小”这样的序列,注意第一个必须是较小的数,问数组a和数组b构成这样一样的序列一共有多少种情况。

分析:

我们考虑到任意的一个数都可以作为起始的那个波谷(相当于它作为第一个数),所以说这样的话以任意一个作为开头的波谷(代码中的num0)的数量都是1,波峰(代码中的num1)的数量都是0.

我们定义sum数组保存的是之前走过的数字中所有的某个位置上的数字可以作为波峰和波谷的方案数。

once数组保存的是在当前的循环中,某个位置上的数字可以作为波峰和波谷的情况数,每次循环这个数组都会被刷新。

这样的话我们往下走,如果在b数组中遇到一个数与当当前的a数组中的这个数相等,那么我们就需要即需要考虑让这个数作为波谷的情况数,也需要考虑让这个数作为波峰的情况数,并且他可以和数组a中的组成的波浪相匹配,两则之和就是累加上的方案数。

如果在b数组中遇到一个数比当前的a数组中的那个数大,也就相当于a数组的那个数要作为波谷,我们在考虑的时候,就需要想到它在本次的时候作为波谷的情况,还有他可以嫁接到之前的波峰上从而作为波谷的情况。

如果在b数组中遇到一个数比当前的a数组中的那个数小的话,就要考虑a数组中的这个数作为波峰的情况,其思考方法与前面的类似。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int max_n=2009;
const int mod=998244353;
int a[max_n],b[max_n];
int n,m,num0,num1,ans;
int once[max_n][2],sum[max_n][2];///once是指以b数组中此次状态下的的第i为作为谷态(0)和峰态(1)的个数
///sum是指以b数组中之前所有的的第j位作为谷态(0)和峰态(1)的个数
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ans=0;
memset(sum,0,sizeof(sum));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
for(int i=1;i<=n;i++)
{
num0=1;///表示此次作为波谷的个数,因为最开始的那个肯定要从波谷开始
num1=0;///表示此次作为波峰的个数
for(int j=1;j<=m;j++)
{
if(a[i]==b[j])///两个相同的话,则进行状态的转移
{
once[j][0]=num0;
once[j][1]=num1;
ans=(ans+(once[j][0]+once[j][1])%mod)%mod;
}
if(a[i]>b[j])///这里作为波峰
{
num1=(num1+sum[j][0])%mod;///这次的可以作为波峰的加上之前的波谷
}
if(a[i]<b[j])
{
num0=(num0+sum[j][1])%mod;///这次的可以作为波谷的加上之前的波峰
}
}
for(int j=1;j<=m;j++)
{
if(a[i]==b[j])
{
sum[j][0]=(sum[j][0]+once[j][0])%mod;
sum[j][1]=(sum[j][1]+once[j][1])%mod;
}
}
}
printf("%d\n",ans);
}
return 0;
}

2017 ACM暑期多校联合训练 - Team 4 1012 HDU 6078 Wavel Sequence (模拟)的更多相关文章

  1. 2017 ACM暑期多校联合训练 - Team 9 1008 HDU 6168 Numbers (模拟)

    题目链接 Problem Description zk has n numbers a1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk gen ...

  2. 2017 ACM暑期多校联合训练 - Team 5 1008 HDU 6092 Rikka with Subset (找规律)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  3. 2017 ACM暑期多校联合训练 - Team 4 1007 HDU 6073 Matching In Multiplication (模拟)

    题目链接 Problem Description In the mathematical discipline of graph theory, a bipartite graph is a grap ...

  4. 2017 ACM暑期多校联合训练 - Team 3 1008 HDU 6063 RXD and math (莫比乌斯函数)

    题目链接 Problem Description RXD is a good mathematician. One day he wants to calculate: ∑i=1nkμ2(i)×⌊nk ...

  5. 2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)

    题目链接 Problem Description We define the distance of two strings A and B with same length n is disA,B= ...

  6. 2017ACM暑期多校联合训练 - Team 6 1003 HDU 6098 Inversion (模拟)

    题目链接 Problem Description Give an array A, the index starts from 1. Now we want to know Bi=maxi∤jAj , ...

  7. 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)

    题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...

  8. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  9. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

随机推荐

  1. (七)对Jmeter进行参数化的俩种方式

    一.使用CSV Data Set Config: 1.添加CSV Data Set Config: 2.配置参数: Filename:文件名,指保存参数化数据的文件目录,可以相对或者绝对路径. Fil ...

  2. oracle 行转列和列转行

    WITH L AS ( ), m AS ( SELECT A.LV AS LV_A, B.LV AS LV_B, TO_CHAR(B.LV) || 'x' || TO_CHAR(A.LV) || '= ...

  3. jdk&tomcat环境变量配置及同时运行多个tomcat方法

    一:jdk配置 安装jdk1.7.0_51,安装过程中所有选项保持默认:最后配置 JDK的环境变量: 在“我的电脑”上点右键—>“属性”—>“高级”—>“环境变量(N)”. 1.新建 ...

  4. Mac & how to uninstall LANDesk

    Mac & how to uninstall LANDesk http://eddiejackson.net/wp/?p=9036 https://community.ivanti.com/d ...

  5. Vue2.0 - 全局操作 Vue.set

    引:http://www.cnblogs.com/zccblog/p/7192420.html Vue.set 的作用就是在构造器外部操作构造器内部的数据.属性或者方法.比如在vue构造器内部定义了一 ...

  6. MVC4中control的增删改查

    public class TestController : Controller { private LeaveEntities db = new LeaveEntities(); // // GET ...

  7. caffe框架下目标检测——faster-rcnn实战篇问题集锦

    1.问题 解决方案:没编译好,需要在lib下编译make 需要在caffe-fast-rcnn下编译make或者make all -j16  ,还需要make pycaffe 2.问题 解决方案:/p ...

  8. AtCoder Regular Contest 083 C: Sugar Water

    题意 给你一个空杯子,有4种操作: 操作1 加100a克的水 操作2 加100b克的水 操作3 加c克的糖 操作4 加d克的糖 糖的质量不能超过水的质量e/100 糖和水的总质量不能超过f 糖的质量不 ...

  9. 【BZOJ5338】[TJOI2018]异或(主席树)

    [BZOJ5338][TJOI2018]异或(主席树) 题面 洛谷 题解 很明显的是\(Trie\)树上暴力判断答案 因为要支持区间,用主席树的结构存\(Trie\)树就好了 #include< ...

  10. 【BZOJ4152】The Captain(最短路)

    [BZOJ4152]The Captain(最短路) 题面 BZOJ Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求 ...