题目链接

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. Django内置的分页模块

    自定义分页 未封装版: 优点:直观 缺点:代码乱,不易维护,可拓展性差 data = [] for i in range(1, 302): tmp = {"id": i, &quo ...

  2. MVC与MVP简单对比

    在Java平台,基于Spring等技术的MVC框架已经走向成熟:在.NET平台,微软也推出了MVC.MVP Framework,MVP不同于MVC的地方,关键在于,View不再显示的依赖于Busine ...

  3. 采用FPGA实现UART转SPI

    应用笔记 V1.1 2015/2/10 采用FPGA实现UART转SPI   概述   本文提供了实现UART转SPI的Verilog代码的功能描述.这份笔记将介绍UART和SPI的基本知识,代码设计 ...

  4. 搭建属于自己的NuGet服务器

    文章导读 创建NuGetServer Web站点 发布站点到IIS 添加本地站点到包包数据源 在上一篇NuGet学习笔记(2) 使用图形化界面打包自己的类库 中讲解了如何打包自己的类库,接下来进行最重 ...

  5. list的迭代器能解决并发问题,collection 的迭代器不能解决并发问题,for可以解决并发问题

    list的迭代器能解决并发问题,collection 的迭代器不能解决并发问题 为什么list支持add,collection不支持 例如有两个人同时添加第三个元素 list的迭代器能锁定线程 只有等 ...

  6. Apache Hadoop YARN – ResourceManager--转载

    原文地址:http://zh.hortonworks.com/blog/apache-hadoop-yarn-resourcemanager/ ResourceManager (RM) is the ...

  7. String Problem HDU - 3374(最大最小表示法+循环节)

    题意: 给出一个字符串,问这个字符串经过移动后的字典序最小的字符串的首字符位置和字典序最大的字符串的首字符的位置,和能出现多少次最小字典序的字符串和最大字典序的字符串 解析: 能出现多少次就是求整个字 ...

  8. 【HDU5730】Shell Necklace(多项式运算,分治FFT)

    [HDU5730]Shell Necklace(多项式运算,分治FFT) 题面 Vjudge 翻译: 有一个长度为\(n\)的序列 已知给连续的长度为\(i\)的序列装饰的方案数为\(a[i]\) 求 ...

  9. BIOS和CMOS的区别

    原文链接:https://www.cnblogs.com/boltkiller/articles/5732424.html 在日常操作和维护计算机的过程中,常常可以听到有关BIOS设置和CMOS设置的 ...

  10. Mobile Service

    link 试题分析 我们发现$dp(t,s1,s2,s3)$表示在$t$时刻$3$个人的位置.发现时间复杂度为$O(n \times L^3)$.不仅会$T$还会$MLE$,所以需要优化$dp$.我们 ...