POJ1426 Find The Multiple —— BFS
2024-09-05 11:54:33
题目链接:http://poj.org/problem?id=1426
Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 34218 | Accepted: 14337 | 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.
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
题解:
题目说答案的位数不会超过100,所以以为是:高精度 + 模运算。这样也太丧心病狂了吧!
后来实在想不到其他方法,就看了题解。结果是一道普通的搜索题,答案用long long存就够了(题目不是说位数<=100),感觉被骗了。
再一次受到了启发:面对一些题目(人生也如此)时,如果想到的方法似乎不能解决问题,但除此之外又无其他办法,那就就要放开手脚试一试,不要畏手畏脚。如果想到的唯一方法都搁置不试,那就相当于交白卷了;试一试,或许能出现意想不到的结果。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; queue<LL>q;
LL bfs(int n)
{
while(!q.empty()) q.pop();
q.push(); while(!q.empty())
{
LL x = q.front();
q.pop();
if(x%n==)
return x;
q.push(1LL*x*);
q.push(1LL*x*+);
}
} int main()
{
int n;
while(scanf("%d",&n) && n)
{
cout<< bfs(n) <<endl;
}
}
最新文章
- php内核分析(三)-全局变量
- nginx 模块讲解
- ASP.NET 服务器控件的生命周期
- mongodb的备份
- MySQL编译安装错误:No curses/termcap library found的解决方法
- java项目导入IntelliJ IDEA
- 图解SQL的inner join、left join、right join、full outer join、union、union all的区别
- hdu 1301 Jungle Roads 最小生成树
- 【转】C++ 智能指针详解
- Node.js学习(10)----文件系统fs
- JQuery无法获取动态添加的图片宽度问题解决办法
- java开发之基础篇2
- 【Demo 0008】Java基础-抽象类
- mongodb时间戳转换成格式化时间戳
- Apache多端口配置
- 201521123065 《Java程序设计》第5周学习总结
- Cup(二分)
- 文本分类实战(四)—— Bi-LSTM模型
- Spring之BeanPostProcessor(后置处理器)介绍
- python批量拷贝文件