C. Two Arrays----思维/dp

173 篇文章 0 订阅

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

the length of both arrays is equal to m;
each element of each array is an integer between 1 and n (inclusive);
ai≤bi for any index i from 1 to m;
array a is sorted in non-descending order;
array b is sorted in non-ascending order.
As the result can be very large, you should print it modulo 109+7.

Input
The only line contains two integers n and m (1≤n≤1000, 1≤m≤10).

Output
Print one integer – the number of arrays a and b satisfying the conditions described above modulo 109+7.

Examples
inputCopy
2 2
outputCopy
5
inputCopy
10 1
outputCopy
55
inputCopy
723 9
outputCopy
157557417
Note
In the first test there are 5 suitable arrays:

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

解析:先从性质走起吧
a数组是非递减的,am就是最大的
b数组是非递增的,bm就是最小的
根据题意ai<=bi,那么am<=bm
那么我们把a,b数组连接起来,怎么连呢?遍历正序a,遍历反序b.
如:a1,a2,a3…am,bm,bm-1…b1;
那么就构成一个2*m长度的新序列,是一个非递减的,每个数的取值范围[1,n].
设f[i][j]
一维:第i个数
二维:第一个数位j
状态方程:f[i][j]=f[i][j+1]+f[i-1][j] .
因为是一个非递减的序列
第i个数为j+1,那么第i-1个数必定为j

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+100;
const int MOD=1e9+7;
typedef long long ll;
ll f[100][1100];
int n,m;
int main()
{
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) f[1][i]=1; //初始化
	for(int i=2;i<=2*m;i++)
	{
		for(int j=n;j>0;j--)
			f[i][j]=(f[i][j+1]+f[i-1][j])%MOD;
	}
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		ans=(ans+f[2*m][i])%MOD;//在该长度为2m数组中最后一个位置放1~n的情况之和
	}
	cout<<ans<<endl;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(function (root, factory, undef) { if (typeof exports === "object") { // CommonJS module.exports = exports = factory(require("./core"), require("./x64-core"), require("./lib-typedarrays"), require("./enc-utf16"), require("./enc-base64"), require("./enc-base64url"), require("./md5"), require("./sha1"), require("./sha256"), require("./sha224"), require("./sha512"), require("./sha384"), require("./sha3"), require("./ripemd160"), require("./hmac"), require("./pbkdf2"), require("./evpkdf"), require("./cipher-core"), require("./mode-cfb"), require("./mode-ctr"), require("./mode-ctr-gladman"), require("./mode-ofb"), require("./mode-ecb"), require("./pad-ansix923"), require("./pad-iso10126"), require("./pad-iso97971"), require("./pad-zeropadding"), require("./pad-nopadding"), require("./format-hex"), require("./aes"), require("./tripledes"), require("./rc4"), require("./rabbit"), require("./rabbit-legacy")); } else if (typeof define === "function" && define.amd) { // AMD define(["./core", "./x64-core", "./lib-typedarrays", "./enc-utf16", "./enc-base64", "./enc-base64url", "./md5", "./sha1", "./sha256", "./sha224", "./sha512", "./sha384", "./sha3", "./ripemd160", "./hmac", "./pbkdf2", "./evpkdf", "./cipher-core", "./mode-cfb", "./mode-ctr", "./mode-ctr-gladman", "./mode-ofb", "./mode-ecb", "./pad-ansix923", "./pad-iso10126", "./pad-iso97971", "./pad-zeropadding", "./pad-nopadding", "./format-hex", "./aes", "./tripledes", "./rc4", "./rabbit", "./rabbit-legacy"], factory); } else { // Global (browser) root.CryptoJS = factory(root.CryptoJS); } }(this, function (CryptoJS) { return CryptoJS; }));解释这段代码
最新发布
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值