题目描述
你需要求出所有长度为 \(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;
}
标签:Distance,int,题解,ABC382D,step,low,ans,include
From: https://www.cnblogs.com/Allen-yang2010/p/18579835