POJ 1426.Find The Multiple
2017-11-17 20:50
323 查看
题目:http://poj.org/problem?id=1426
AC代码(C++):
总结: 很坑的一道题目, 如果不用打表的话. 看懂题目后直接想到的就是用bfs来做, bfs队列里存放长度为100的字符串, 每次分两种情况, 在字符串后面接1或者接0. 这样做提交之后MLE了, 想着应该是字符串太大了. 看了很多解题报告后发现, 解的长度在无符号64位int内可以表示(经打表验证, 长度最长的是n=198时共18位), 于是直接用unsigned __int64代替字符串来bfs, 结果超时了(所以又是STL背锅吗). 然后换成dfs就AC了(很迷).
AC代码(C++):
#include <iostream> #include <algorithm> #include <stdio.h> #include <vector> #include <queue> #include <math.h> #include <string> #include <string.h> #include <bitset> #define INF 0xfffffff #define MAXN 100005 using namespace std; int n; unsigned __int64 ans; bool flag; void dfs(unsigned __int64 x, int deepth){ if(deepth>19||flag)return; if(x%n==0){ ans = x; flag = true; } else{ dfs(x*10,deepth+1); dfs(x*10+1,deepth+1); } } int main(){ while(cin>>n){ if(n==0)break; flag = false; dfs(1,1); cout<<ans<<endl; } }
总结: 很坑的一道题目, 如果不用打表的话. 看懂题目后直接想到的就是用bfs来做, bfs队列里存放长度为100的字符串, 每次分两种情况, 在字符串后面接1或者接0. 这样做提交之后MLE了, 想着应该是字符串太大了. 看了很多解题报告后发现, 解的长度在无符号64位int内可以表示(经打表验证, 长度最长的是n=198时共18位), 于是直接用unsigned __int64代替字符串来bfs, 结果超时了(所以又是STL背锅吗). 然后换成dfs就AC了(很迷).
相关文章推荐
- POJ - 1426 - Find The Multiple (DFS)
- poj 1426 Find The Multiple
- POJ-1426 Find The Multiple
- POJ1426 Find The Multiple 解题报告
- poj 1426 Find The Multiple
- Find The Multiple (poj 1426 bfs)
- POJ 1426 Find The Multiple(BFS)
- POJ 1426 Find The Multiple
- poj-1426 Find The Multiple
- D - Find The Multiple POJ - 1426 ——DFS || BFS
- poj 1426 Find The Multiple(DFS)
- DFS/BFS(同余模) POJ 1426 Find The Multiple
- [kuangbin带你飞]专题一 简单搜索 E - Find The Multiple poj 1426
- POJ 1426 Find The Multiple(打表)
- POJ_1426_Find The Multiple
- POJ 1426 Find The Multiple(BFS)
- POJ1426 Find The Multiple(bfs)
- poj 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ1426-Find The Multiple