Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26926   Accepted: 11174   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 题意:找到一个由01组成的串m使得 m%n == 0
题解:同余定理.很强的搜索剪枝技巧。
同余剪枝,如果一个小数的余数和大数的余数相同,假设小点的数 a ,大点的为 b ,a%n==b%n => (a+c)%n == (b+c)%n ,我们假设 a+c是符合条件的,那么,b+c 也是符合条件的,所以我们可以直接不要
b+c 了,这就是一个很巧妙并且很强的剪枝.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
int n;
bool mod[]; ///这里是最强的剪枝,同余剪枝,如果一个小数的余数和大数的余数相同
///假设小点的数 a ,大点的为 b ,a%n==b%n => (a+c)%n == (b+c)%n ,我们假设 a+c是符合条件的,那么
/// b+c 也是符合条件的,所以我们可以直接不要 b+c 了,这就是一个很巧妙并且很强的剪枝.
struct Node{
int mod;
string ans;
};
string bfs(){
memset(mod,false,sizeof(mod));
queue<Node> q;
Node s;
s.ans = "";
s.mod = %n;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.mod==){
return now.ans;
}
Node next;
next.mod = (now.mod*+)%n;
next.ans = now.ans+"";
if(!mod[next.mod]){
mod[next.mod] = true;
q.push(next);
}
next.mod = (now.mod*)%n;
next.ans = now.ans+"";
if(!mod[next.mod]){
mod[next.mod] = true;
q.push(next);
}
}
}
int main(){ while(scanf("%d",&n)!=EOF,n){
cout<<bfs()<<endl;
}
}

poj 1426(同余搜索)的更多相关文章

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

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

  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(寻找倍数)

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

  4. POJ.1426 Find The Multiple (BFS)

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

  5. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  6. hdu 1226(同余搜索)

    超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

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

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

  8. POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2

    http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...

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

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

随机推荐

  1. bzoj4518: [Sdoi2016]征途(DP+决策单调性分治优化)

    题目要求... 化简得... 显然m和sum^2是已知的,那么只要让sigma(si^2)最小,那就变成了求最小平方和的最小值,经典的决策单调性,用分治优化即可. 斜率优化忘得差不多就不写了 #inc ...

  2. bzoj1045: [HAOI2008] 糖果传递(思维题)

    首先每个人一定分到的糖果都是所有糖果的平均数ave. 设第i个人给i-1个人Xi个糖果,则有Ai-Xi+X(i+1)=ave. 则A1-X1+X2=ave,A2-X2+X3=ave,A3-X3+X4= ...

  3. squid总结

    squid可以完成的工作: 代理服务器 反向代理服务器 防火墙 缓存功能 透明代理 squid和varnish的对比,以及squid的优缺点说明: 缓存到硬盘,容易遇到I/O瓶颈 V3.2以下不支持多 ...

  4. 题解【luoguP3644 [APIO2015]八邻旁之桥】

    题目链接 题解 家和公司在同侧 简单,直接预处理掉 若 \(k=1\) 取所有的居民的\(\frac{家坐标+公司坐标}{2}\)的所有坐标的正中间建一座桥,使所有居民到的距离最小. 实现方法:线段树 ...

  5. ACM1558两线段相交判断和并查集

    Segment set Problem Description A segment and all segments which are connected with it compose a seg ...

  6. Installing JDK7 on Ubuntu

    1. # sudo add-apt-repository ppa:webupd8team/java 2.  # sudo apt-get update 3.  # sudo apt-get insta ...

  7. 修改Maven仓库地址

    在%USERPROFILE%\.m2\settings.xml例如:C:\Users\LongShu\.m2\settings.xml 可以自定义Maven的一些参数, 复制%M2_HOME%\con ...

  8. uoj308 【UNR #2】UOJ拯救计划

    传送门:http://uoj.ac/problem/308 [题解] 考虑枚举用了$i$所学校,那么贡献为${k \choose i} * cnt * i!$ 意思是从$k$所选$i$所出来染色,$c ...

  9. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

  10. Python与RPC -- (转)

    XML-RPC xmlrpc是使用http协议做为传输协议的rpc机制,使用xml文本的方式传输命令和数据. 一个rpc系统,必然包括2个部分: 1)rpc client,用来向rpc server调 ...