Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21436   Accepted: 8775   Special Judge

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal
digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

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 &amp;&amp; 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)的更多相关文章

  1. 51nod 1109 01组成的N的倍数

    用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道:  ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...

  2. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  3. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  4. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  5. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  6. DFS/BFS(同余模) POJ 1426 Find The Multiple

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

  7. POJ - 1426 Find The Multiple(搜索+数论)

    转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...

  8. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  9. POJ 1426 Find The Multiple (DFS / BFS)

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

随机推荐

  1. final修饰符的三种使用场景

    final有三种使用场景,各自是修饰变量.方法和类.不管哪种修饰.一旦声明为final类型.你将不能改变这个引用了,编译器会检查代码,假设你试图再次初始化,编译器会报错.以下我来详细说说每一种修饰场景 ...

  2. avro序列化详细操作

    Intellij 15.0.3 Maven avro 1.8.0 Avro是一个数据序列化系统. 它提供以下: 1 丰富的数据结构类型 2 快速可压缩的二进制数据形式 3 存储持久数据的文件容器 4 ...

  3. Spark(十二) -- Spark On Yarn & Spark as a Service & Spark On Tachyon

    Spark On Yarn: 从0.6.0版本其,就可以在在Yarn上运行Spark 通过Yarn进行统一的资源管理和调度 进而可以实现不止Spark,多种处理框架并存工作的场景 部署Spark On ...

  4. 解决 The &#39;InnoDB&#39; feature is disabled; you need MySQL built with &#39;InnoDB&#39; to have it working

    事由: 迁移server的时候须要操作数据库.将数据库也进行迁移,在新server中导入数据的时候提示 The 'InnoDB' feature is disabled; you need MySQL ...

  5. iOS tableView下拉图片放大

    事实上这个效果,本质上就是在你tableView下拉 造成offset时候. 保持你顶部图片的y坐标点还停留在下拉时屏幕的顶点(offset), 而图片的长度变为原始的height长度-(offset ...

  6. openerp config file

    [options] addons_path = /bin/openerp/addonsadmin_passwd = admincsv_internal_sep = , db_host = False ...

  7. UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

    UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO.最后一个放的人赢.问谁赢 思路:sg函数.每一段.. ...

  8. LoadRunner监控tomcat

    LoadRunner监控tomcat (2012-10-25 14:01:42)转载▼ double atof (const char * string);Action(){    // 保存JVM内 ...

  9. 《深入PHP:面向对象、模式与实践》(二)

    第4章 高级特性 本章内容提要: 静态属性和方法:通过类而不是对象来访问数据和功能 抽象类和接口:设计和实现分离 错误处理:异常 Final类和方法:限制继承 拦截器方法:自动委托 析构方法:对象销毁 ...

  10. BOS中定区关联客户

    1. 首先发布crm服务 第一步:创建动态的web项目crm,导入hessian的jar 第二步:创建一个crm数据库和t_customer表 第三步:在crm项目的web.xml中配置spring的 ...