您的位置:首页 > 其它

POJ 1426.Find The Multiple

2017-11-17 20:50 323 查看
题目: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 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了(很迷).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: