51nod1109 01组成的倍数 数据结构
2016-01-25 13:20
615 查看
题目:
给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示,是由0和1组成的。求最小的M。
例如:N = 4,M = 100。
Input
Output
Input示例
Output示例
思路:因为n比较大,倍数肯定会超long long ,所以设计一个数组,记录每一位的数据,和每一位的前面的位置下标即可,找到答案,DFS输出即可。
给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示,是由0和1组成的。求最小的M。
例如:N = 4,M = 100。
Input
输入1个数N。(1 <= N <= 10^6)
Output
输出符合条件的最小的M。
Input示例
4
Output示例
100
思路:因为n比较大,倍数肯定会超long long ,所以设计一个数组,记录每一位的数据,和每一位的前面的位置下标即可,找到答案,DFS输出即可。
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <fstream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <map> #include <set> #include <iomanip> using namespace std; #define maxn 2000005 #define MOD 1000000007 #define mem(a , b) memset(a , b , sizeof(a)) #define LL long long #define INF 1000000000 struct node { int x ,per; }ans[maxn]; bool vis[maxn]; int n , num; void DFS(int pos) { int res = ans[pos].per; if(res <= 0) { printf("1"); return; } DFS(res); printf("%d" , ans[res].x); } void BFS() { num = 1; mem(vis , 0); node cur , tmp; cur.x = 1 , cur.per = 0; queue<node>q; ans[0].x = 1; ans[0].per = 0; while(!q.empty()) q.pop(); q.push(cur); while(!q.empty()) { tmp = q.front(); q.pop(); for(int i = 0 ; i <= 1 ; i ++) { cur.x = tmp.x * 10 + i; cur.x %= n; if(!vis[cur.x]) { cur.per = num; vis[cur.x] = 1; ans[num].x = i; ans[num].per = tmp.per; q.push(cur); if(cur.x == 0) { DFS(num); printf("%d\n" , i); return; } num++; } } } } int main() { while(scanf("%d" , &n) != EOF) { if(n == 1) { printf("1\n"); continue; } BFS(); } return 0; }
相关文章推荐
- 七.学习数据结构之树
- C++ —— 数据结构之 顺序栈,链式栈?
- UVA 12096 The SetStack Computer
- 六.学习数据结构之递归
- opencv从入门到精通(4)--IplImage数据结构
- 学习STL map, STL set之数据结构基础
- 2015年大二上-数据结构-图-1-(4)-利用遍历思想求解图问题(2)
- 2015年大二上-数据结构-图-1-(4)-利用遍历思想求解图问题(1)
- 数据结构(寒假小结)——2.线性表
- 小蚂蚁学习数据结构(23)——伪代码中的引用
- PHP——数组和数据结构
- LeetCode12. Integer to Roman不用任何数据结构和数组
- 查找链表倒数第K个节点
- 再看数据结构之KMP匹配
- 数据结构与算法——二叉树高度(C语言)
- 数据结构与算法知识大纲
- 数据结构基础(四)树和二叉树
- 数据结构实现之索引优先队列用例(多路归并)
- 数据结构实现之最大索引优先队列
- 数据结构实现之最小索引优先队列