【题解】ABC382D Keep Distance

题目描述

你需要求出所有长度为 \(n\),且满足以下条件的序列的个数,并按照字典序输出:
首项大于等于 \(1\), 每一项比前一项至少大 \(10\),最后一项小于等于 \(m\)

题目分析

爆搜,用 vector 存储答案和个数,输出。

代码实现

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Node{
	int a[15];
};
vector<Node> res;
int ans[15];
int n, m;
void dfs(int step){
	if(step == n + 1){
		if(ans[n] > m)return;
		Node x;
		for(int i = 1; i <= n; i ++){
			x.a[i] = ans[i];
		}
		res.push_back(x);
		return;
	}
	int low;
	if(step > 1)low = ans[step - 1] + 10;
	else low = 1;
	for(int i = low; i <= m - (n - step) * 9; i ++){
		ans[step] = i;
		dfs(step + 1);
	}
}
int main(){
	cin >> n >> m;
	dfs(1);
	cout << res.size() << "\n";
	for(auto it : res){
		for(int i = 1; i <= n; i ++){
			cout << it.a[i] << " ";
		}
		cout << "\n";
	}
	return 0;
}
posted @ 2024-12-01 15:41  Allenyang_Tco  阅读(8)  评论(0编辑  收藏  举报