POJ 1426 Find The Multiple && 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 21436 | Accepted: 8775 | Special Judge |
Description
digits.
Input
Output
Sample Input
- 2
- 6
- 19
- 0
Sample Output
- 10
- 100100100100100100
- 111111111111111111
Source
题意:输入一个正整数n(1<=n<=200),然后要求找一个仅仅包括0和1的十进制数字能整除n
- #include<iostream>
- #include<algorithm>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<queue>
- using namespace std;
- int n;
- int ans;
- int v[5000];
- struct node
- {
- int x;
- int y;
- } a[1000010];
- void DFS(int k)
- {
- int pt = a[k].y;
- if(pt <= 0)
- {
- printf("1");
- return ;
- }
- DFS(pt);
- printf("%d",a[pt].x);
- }
- void BFS()
- {
- ans = 1;
- memset(v,0,sizeof(v));
- queue<node>q;
- struct node t,f;
- t.x = 1;
- t.y = 0;
- a[0].x = 1;
- a[0].y = 0;
- q.push(t);
- while(!q.empty())
- {
- t = q.front();
- q.pop();
- for(int i=0; i<=1; i++)
- {
- f.x = t.x * 10 + i; /// 同余模定理应用
- if(v[f.x] == 0)
- {
- f.x = f.x % n;
- f.y = ans;
- q.push(f);
- v[f.x] = 1;
- a[ans].x = i;
- a[ans].y = t.y;
- if(f.x == 0)
- {
- DFS(ans);
- printf("%d\n",i);
- return ;
- }
- ans++;
- }
- }
- }
- }
- int main()
- {
- while(scanf("%d",&n)!=EOF)
- {
- if(n == 0)
- {
- break;
- }
- BFS();
- }
- return 0;
- }
51nod 1109
- #include<iostream>
- #include<algorithm>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<queue>
- using namespace std;
- int n;
- int ans;
- int v[1010000];
- struct node {
- int x;
- int y;
- } a[1000010];
- void DFS(int k) {
- int pt = a[k].y;
- if(pt <= 0) {
- printf("1");
- return ;
- }
- DFS(pt);
- printf("%d",a[pt].x);
- }
- void BFS() {
- ans = 1;
- memset(v,0,sizeof(v));
- queue<node>q;
- while(!q.empty()){
- q.pop();
- }
- struct node t,f;
- t.x = 1;
- t.y = 0;
- a[0].x = 1;
- a[0].y = 0;
- q.push(t);
- while(!q.empty()) {
- t = q.front();
- q.pop();
- for(int i=0; i<=1; i++) {
- f.x = t.x * 10 + i; /// 同余模定理应用
- f.x = f.x % n;
- if(v[f.x] == 0) {
- f.y = ans;
- q.push(f);
- v[f.x] = 1;
- a[ans].x = i;
- a[ans].y = t.y;
- if(f.x == 0) {
- DFS(ans);
- printf("%d\n",i);
- return ;
- }
- ans++;
- }
- }
- }
- }
- int main() {
- while(scanf("%d",&n)!=EOF) {
- BFS();
- }
- return 0;
- }
POJ 1426 Find The Multiple && 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)的更多相关文章
- 51nod 1109 01组成的N的倍数
用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道: ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- DFS/BFS(同余模) POJ 1426 Find The Multiple
题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...
- POJ - 1426 Find The Multiple(搜索+数论)
转载自:優YoU http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...
- poj 1426 Find The Multiple( bfs )
题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...
- POJ 1426 Find The Multiple (DFS / BFS)
题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...
随机推荐
- AtomicInteger类的简单应用
AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而AtomicIn ...
- http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml
http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml
- http://blog.csdn.net/huang_xw/article/details/7090173
http://blog.csdn.net/huang_xw/article/details/7090173
- python实现word2vec训练结果bin文件转txt文件
经理让我把word2vec训练后得到的bin文件转为txt文件,目前还不知道txt文件用来干什么.其实word2vec训练语料时可以选择训练处出bin文件或者txt文件,但是训练出bin文件时过程太漫 ...
- DevExpress.Build
using System.Collections.Generic; using Microsoft.Build.AppxPackage; using Microsoft.Build.Framework ...
- XAMPP Apache + MySQL + PHP + Perl
XAMPP Apache + MySQL + PHP + Perl 什么是XAMPP? XAMPP是最流行的PHP开发环境 XAMPP是完全免费且易于安装的Apache发行版,其中包含MySQL.PH ...
- yoman搭建你的react-webpack应用
还没有npm和node的要提前做好准备工作 做好这一切之后 我们安装yo,记住安装在全局变量中,我们需要用他的命令工具 npm install -g yo 接下来安装yo提供的react-webpac ...
- [Unity3D]UI方案及制作细节(NGUI/EZGUI/原生UI系统)
转载请留下本文原始链接,谢谢.本文会不定期更新维护,最近更新于2013.09.17. http://blog.sina.com.cn/s/blog_5b6cb9500101bplv.html ...
- 有关于apktool的使用的一些心得
<span style="font-family: Arial, Helvetica, sans-serif;">1.配置Java的环境</span> 1) ...
- Python——在Python中如何使用Linux的epoll
在Python中如何使用Linux的epoll 目录 序言 阻塞socket编程示例 异步socket的好处以及Linux epoll 带epoll的异步socket编程示例 性能注意事项 源代码 序 ...