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 ...
随机推荐
- SQLSERVER调用DLL程序
在SQL Server中调用dll分为两个步骤 1.创建一个dll文件(dll文件分成3种类型,讲其中一种) 2.把dll文件放进SQL Server的程序集中.然后定义一个Function,就可以通 ...
- DEDECMS爆严重安全漏洞
简要描述: 众所周知,因使用简单.客户群多,织梦CMS一直被爆出许多漏洞.“DEDECMS爆严重安全漏洞,近期官方会发布相关补丁,望大家及时关注补丁动态.” 详细说明: http://www.xx.c ...
- HTTP/2 Server Push 详解(上)
收录待用,修改转载已取得腾讯云授权 译者:TAT.Johnny 原文:https://www.smashingmagazine.com/2017/04/guide-http2-server-push/ ...
- 关于 iOS 证书,你必须了解的知识
收录待用,修改转载已取得腾讯云授权 最新腾讯云技术公开课直播,提问腾讯W3C代表,如何从小白成为技术专家?点击了解活动详情. 作者 |陈泽滨 编辑 | 顾乡 从事iOS开发几年,越来越发现,我们的开发 ...
- stat,查看文件属性
shell命令,查看文件详细属性 别再跟我回答ls -l了
- Foundation框架 - NSNumber类
NSNumber类 NSFormatter #import <Foundation/Foundation.h> int main(int argc, const char * argv[] ...
- 字符串去重(hashSet)
public static String deleteRepeat(String strn){ String s=strn; String[] array = s.sp ...
- nmap小技巧[1] 探测大网络空间中的存活主机
url: nmap是所有安全爱好者应该熟练掌握的扫描工具,本篇介绍其在扫描大网络空间时的用法. 为什么要扫描大网络空间呢? 有这样的情形: 内网渗透 攻击者单点突破,进入内网后,需进一步扩大成果, ...
- 一张图片教会你写mysql 语句
MySQL的语句执行顺序 MySQL的语句一共分为11步,如下图所标注的那样,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入 ...
- JavaScript完整性检查
1.7个“坑” <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...