题解:AT_abc367_c [ABC367C] Enumerate Sequences
Do_not_hit_plane · · 题解
C-Enumerate Sequences
题目大意
按升序排列,打印所有满足以下条件的长度为 N 的整数序列。
- 第 i 个元素介于 1 和
Ri (含)之间。 -
所有元素之和是 K 的倍数。
思路
因为数据很水,所以使用dfs爆搜即可。
AC代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){//快读
char c;
int f=1,res=0;
while(c=getchar(),!isdigit(c)) if(c=='-') f*=-1;
while(isdigit(c)) res=res*10+c-'0',c=getchar();
return res*f;
}
int n,k,r[10];
int a[10];
string str[1000000];
bool check(){//判断是否满足条件
int t=0;
for(int i=1;i<=n;i++){
if(a[i]>r[i]){
return 0;
}
t+=a[i];
}
if(t%k==0) return 1;
return 0;
}
int now=1;
void dfs(int step){//爆搜
if(step==n+1){
if(check()){
for(int i=1;i<=n;i++){
str[now]+=a[i]+'0';
}
now++;
}
return;
}
for(int i=1;i<=r[step];i++){
a[step]=i;
dfs(step+1);
}
}
int main(){
n=read();
k=read();
for(int i=1;i<=n;i++){
r[i]=read();
}
dfs(1);
sort(str,str+now);//排序
for(int i=1;i<now;i++){
for(int j=0;j<str[i].size();j++){
cout<<str[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}