C. Two Arrays

3 篇文章 0 订阅

You are given two integers nn and mm. Calculate the number of pairs of arrays (a,b)(a,b) such that:

  • the length of both arrays is equal to mm;
  • each element of each array is an integer between 11 and nn (inclusive);
  • ai≤biai≤bi for any index ii from 11 to mm;
  • array aa is sorted in non-descending order;
  • array bb is sorted in non-ascending order.

As the result can be very large, you should print it modulo 109+7109+7.

Input

The only line contains two integers nn and mm (1≤n≤10001≤n≤1000, 1≤m≤101≤m≤10).

Output

Print one integer – the number of arrays aa and bb satisfying the conditions described above modulo 109+7109+7.

Examples

input

Copy

2 2

output

Copy

5

input

Copy

10 1

output

Copy

55

input

Copy

723 9

output

Copy

157557417

Note

In the first test there are 55 suitable arrays:

  • a=[1,1],b=[2,2]a=[1,1],b=[2,2];
  • a=[1,2],b=[2,2]a=[1,2],b=[2,2];
  • a=[2,2],b=[2,2]a=[2,2],b=[2,2];
  • a=[1,1],b=[2,1]a=[1,1],b=[2,1];
  • a=[1,1],b=[1,1]a=[1,1],b=[1,1].

题意:

       给出2个整数n,m,然后让你构建2个数列a,b,数列长度都为m,每个a[i],b[i]都属于[1,n],数列a非递减,数列b非递增,b[i]>=a[i],问你有多少种构造方法。

思路:

       很明显是一道DP题,看代码把= =

code:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL MOD = 1e9+7;
LL n,m,dp1[1001][11],dp2[1001][11],ans = 0;
int main()
{
	cin>>n>>m;
	for(int i=1; i <= n; i++)
	{
		dp1[i][1] = 1;
		dp2[i][1] = 1;
	}
	for(int i = 2; i <= m; i++)
		for(int j = 1; j <= n; j++)
			for(int k = 1; k <= j;k++)
			dp1[j][i] = (dp1[j][i]+dp1[k][i-1])%MOD;
	for(int i = 2; i <= m; i++)
		for(int j = 1; j <= n; j++)
			for(int k = j; k <= n; k++)
				dp2[j][i] = (dp2[j][i]+dp2[k][i-1])%MOD;
	for(int i=1; i <= n; i++)
		for(int j=i; j <= n; j++)
			ans = (ans+dp1[i][m]*dp2[j][m])%MOD;
	cout<<ans<<endl;
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了解决这个问题,我们可以先对两个数组进行排序,然后用双指针法来找到所有相似的子数组。具体实现如下: ```c #include <stdio.h> #include <stdlib.h> #define MAX_N 1000000 int M[MAX_N], N[MAX_N]; int m, n; int compare(const void *a, const void *b) { return *(int*)a - *(int*)b; } int count_similar() { int i = 0, j = 0; int count = 0; while (i < m && j < n) { if (M[i] == N[j]) { // 找到相同的元素 int k = j; while (k < n && N[k] == N[j]) { k++; } count += k - j; // 加上与N[j]相同的元素的个数 i++; j = k; } else if (M[i] < N[j]) { // M[i]较小,需要增加N中的元素 i++; } else { // M[i]较大,需要减少N中的元素 j++; } } return count; } int main() { scanf("%d%d", &m, &n); for (int i = 0; i < m; i++) { scanf("%d", &M[i]); } for (int i = 0; i < n; i++) { scanf("%d", &N[i]); } qsort(M, m, sizeof(int), compare); qsort(N, n, sizeof(int), compare); printf("%d\n", count_similar()); return 0; } ``` 首先读入两个数组的长度和元素,然后对它们进行排序。然后用双指针法找到所有相似的子数组,具体方法如下: 1. 初始化两个指针i和j,分别指向M和N的开头; 2. 如果M[i]等于N[j],说明找到了一个相似的子数组,接着向右移动j,直到N[j]不等于N[j+1]; 3. 如果M[i]小于N[j],说明N[j]需要增加,所以向右移动i; 4. 如果M[i]大于N[j],说明N[j]需要减少,所以向右移动j; 5. 重复2~4步,直到i到达M的末尾或j到达N的末尾。 遍历过程中,我们用一个计数器count来累计相似的子数组的个数,每当找到一个相似的子数组时,我们就把与N[j]相同的元素的个数加到count中。最后返回count即可。 时间复杂度为O(MlogM + NlogN),空间复杂度为O(M + N)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值