ZOJ-1530

来源:互联网 发布:淘宝卡通行走人偶 编辑:程序博客网 时间:2024/06/05 04:02

基础DFS,但是最开始我加了余数的flag数组来回溯,就TLE,不知道怎么回事。。看了别人的代码发现完全不用这个数组直接暴搜竟然能过。。太诡异了

#include<cstdio>#include<vector>#include<cstring>using namespace std;namespace{vector<bool> res;bool find;int n;void dfs(int num){if (find || res.size() > 100)return;if (!num){find = true;for (size_t i = 0; i != res.size(); i++)printf("%d", res[i] ? 1 : 0);putchar('\n');}for (int i = 0; i < 2; i++){int temp = (num * 10 + i) % n;res.push_back(i);dfs(temp);res.pop_back();}}}int main(){while (scanf("%d", &n), n){find = false;res.push_back(1);dfs(1);res.pop_back();}return 0;}


0 0