标签:for while fine make scanf class nod inf algorithm
输入1个数N。(1 <= N <= 10^6)
输出符合条件的最小的M。
4
100
思路:最开始一直在想构造方法,歪了。想到同余定理就是简单题了,bfs一下,不断末尾添加‘0‘或‘1‘,保存一下当前余数,如果访问过就跳过。总复杂度大概nlog(n)。log是因为在bfs时向队列传入的字符
串长度会达到log级别。
#include <iostream> #include <fstream> #include <sstream> #include <cstdlib> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <vector> #include <set> #include <map> #include <iomanip> #include <cctype> #include <cassert> #include <bitset> #include <ctime> using namespace std; #define pau system("pause") #define ll long long #define pii pair<int, int> #define pb push_back #define mp make_pair #define clr(a, x) memset(a, x, sizeof(a)) const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const double EPS = 1e-9; struct gg { string s; int a; gg(){} gg(string s, int a) : s(s), a(a) {} }; bool vis[1000015]; queue<gg> que; int n; int main() { scanf("%d", &n); if (1 == n) { printf("1"); return 0; } que.push(gg("1", 1 % n)); vis[1] = 1; while (que.size()) { gg g = que.front(); que.pop(); for (int i = 0; i < 2; ++i) { string s = g.s + (char)(‘0‘ + i); int a = (g.a * 10 + i) % n; if (!a) { cout << s; return 0; } if (vis[a]) continue; que.push(gg(s, a)); vis[a] = 1; } } return 0; }
标签:for while fine make scanf class nod inf algorithm
原文地址:http://www.cnblogs.com/BIGTOM/p/7898450.html