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 ...
随机推荐
- iOS:Masonry练习详解
Masonry练习详解 添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConst ...
- ubuntu16.04画图软件kolourpaint
1.安装kolourpaint sudo apt-get install kolourpaint4 -y 在里面搜索“kolourpaint”这个软件名.
- Java ArrayList的模拟实现
package test; import java.util.Arrays; import java.util.Collection; public class MyArrayList<E> ...
- Android Exception18(Stuido debug .....)
这个问题比较诡异,在用android-studio debug的时候,第一次能正常使用,但是后面就不知道是什么鬼,每次debug都冒出来这个. 之后,重新新建一个项目就好了
- Linux下配置两个或多个Tomcat启动
Linux下配置两个或多个Tomcat启动 (2012-08-14 11:59:31) 转载▼ 标签: 杂谈 分类: linux_tomcat 步骤如下: (1)修改/etc/profile文件.添加 ...
- PL/SQL 异常错误处理
异常错误处理 一个优秀的程序都应该可以正确处理各种出错情况,并尽可能从错误中恢复.ORACLE 提供异常情况(EXCEPTION)和异常处理(EXCEPTION HANDLER)来实现错误处理 ...
- 分别在.NET Framework 与 .NET Core 框架下 编写Windows Service(windows服务程序)
前言,为什么会分别在两个框架下编写Windows Service,是因为最近在做区块链这块,使用的是NEO(小蚁区块链)的相关技术,NEO使用的是.net core 2.1,业务上需要写两个程序,一个 ...
- Sql Server分页分段查询百万级数据四种项目实例
实际项目中需要实现自定义分页,最关键第一步就是写分页SQL语句,要求语句效率要高. 那么本文的一个查询示例是查询第100000-100050条记录,即每页50条的结果集.查询的表名为infoTab,且 ...
- c++opencv项目移植到Android(Mat—》IplImage*)
近期将PC机上的行人检測的C++项目移植到Android平台遇到非常多的问题.因此,记录一些重点. 1. 最好能够參照Opencv sample 里面的mix-processing. 2. 数据结构的 ...
- 腾讯云 net.core
搭建 .NET Core 开发环境 安装 .Net Core 执行代码 任务时间:时间未知 .NET Core 的官方文档很详细,本实验带你建立一个.NET Core 1.1的Web运行环境,更多内容 ...