51 nod 01组成的倍数 1109

 

                      1,首先就是 (13%3=((1×10)%3+3)%3

                  用同余定理,如果a%n=c,b%n=c,a<b,那么这个时候b就不用入队了,因为答案只看余数(这是第一种剪枝,
                      2,另外一个剪枝就是相同的余数不必继续入队列
                      3,上代码了
                      4()
                      

#include <algorithm>
#include <stdio.h>
#include <stack>
#include <istream>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define MAX 2005
using namespace std;
const int maxn = 20000007;
int vis[maxn];
struct cnm{
vector <char> inof;
int weishu;
}temp,now;
queue <cnm> lb;
int main() {
int n;
scanf("%d", &n);
if (n == 1) {
printf("1\n"); return 0;
}
bool ans = 1;
memset(vis, 0, sizeof(vis));
vis[1] = 1;
now.weishu = 1;
now.inof.push_back('1');
lb.push(now);
while (true) {
now = lb.front(); lb.pop();
int a = now.weishu * 10;
for (int i = 0; i < 2; i++) {
int b = (a + i) % n;
if (vis[b])continue;
if (b == 0) {
for (int j = 0; j < now.inof.size(); j++) {
printf("%c", now.inof[j]);
}
printf("%d\n", i);
ans = 0;
}
vis[b] = 1;
temp = now;
temp.inof.push_back(i+'0');
temp.weishu = b;
lb.push(temp);
}
if (!ans)break;
}
return 0;
}

posted @ 2018-04-11 19:52  Xzavieru  阅读(157)  评论(0编辑  收藏  举报