POJ 1426.Find The Multiple

来源:互联网 发布:ios移动网络下上传图片 编辑:程序博客网 时间:2024/06/07 00:05

题目:http://poj.org/problem?id=1426

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 100005using 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了(很迷).

原创粉丝点击