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 ...
随机推荐
- dd-wrt端口映射不出去的解决办法
本人有一个巴法络的WZR-HP-G450H系统自带的固件不好用,但是随机却带了一个官方定制的DD-WRT,于是刷了去,但是今天在做一个FTP的时候突然无论怎么样映射或是做DMZ都不会出去,终于找到解决 ...
- Hive使用简介
---恢复内容开始--- 指定分隔符 HIVE输出到文件的分隔符 ,列与列之间是'\1'(ASCII码1,在vim里显示为^A),列内部随着层数增加,分隔符依次为'\2','\3','\4'等. 例: ...
- vue-resource的使用中需要注意的坑
先看一段代码: export default { name: 'app', data() { return { articles: [] } }, created: function() { this ...
- KVO(Key Value Observing)
*KVO能够监听某个对象属性的改变 原理:仅仅要给一个对象注冊一个监听,那么在执行时, 系统就会自己主动给该对象生成一个子类对象,而且重写自己主动生成的子类对象的被监听属性的set方法.然后在set方 ...
- webmagic 初始化 startRequests
在spider类中有三个方法可以初始化startRequests.可以对这些地方进行扩展. /** * create a spider with pageProcessor. * * @param p ...
- float过后 高度无法自适应的解决方法
float过后 高度无法自适应的解决方法: 在float层最外面包一层div即可.
- 微信小程序页面跳转
一:跳转的数据传递 例如:wxml中写了一个函数跳转: [html] view plain copy <view class="itemWeight" catchtap=&q ...
- null和undefined相等比较
在==(相等)判断中,null和undefined相等(它们也与自身相等),除此之外不与其他值相等. 示例代码: <!DOCTYPE html> <html lang="z ...
- Objective-C之成魔之路【19-归档】
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠.支持郝萌主,捐赠数额任意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 在Obje ...
- KINavigationController使用演示例子
代码地址如下:http://www.demodashi.com/demo/12905.html 运行效果 实现思路 创建pan手势,添加到页面中,监听手势的动作.重写push的方法,在push之前截图 ...