判断到根号n即可,另外使用dfs输出,不需要另开数组再排序。
#include<cmath>
#include<cstdio>
int P, L, len, cnt;
void dfs(int dep){
if(dep > len) return;
if(dep > L){
if(P % dep == 0){
if(cnt == 1) printf("%d", dep), cnt = 2;
else printf(" %d", dep);
dfs(dep+1);
if(P/dep != dep && P/dep > L) {
if(cnt == 1) printf(" %d", P/dep), cnt = 2;
else printf(" %d", P/dep);
}
}else dfs(dep+1);
}else {
dfs(dep+1);
if(P % dep == 0 && P/dep > L) {
if(cnt == 1) printf("%d", P/dep), cnt = 2;
else printf(" %d", P/dep);
}
}
}
int main(){
int t, CASE(0);
scanf("%d", &t);
while(t--){
scanf("%d%d", &P, &L);
if(L >= P - L) printf("Case %d: impossible\n", ++CASE);
else {
P -= L;
len = sqrt(P);
printf("Case %d: ", ++CASE);
cnt = 1;
dfs(1);
printf("\n");
}
}