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

测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储

从1出发,每次向10*s和10*s+1转移,只存储余数即可,

对于余数i,肯定只有第一个余数为i的最有用,只记录这个值即可

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=222;
typedef long long ll;
ll dp[maxn];
int n;
queue<int >que;
int main(){
while(scanf("%d",&n)==1&&n){
if(n==1){puts("1");continue;}
memset(dp,-1,sizeof(dp));
while(!que.empty())que.pop();
dp[1]=1;
que.push(1);
bool fl=false;
while(!que.empty()){
int s=que.front();que.pop();
if(s==0){
printf("%I64d\n",dp[s]);
fl=true;
break;
}
int t=s*10%n;
if(dp[t]==-1){
dp[t]=10*dp[s];
que.push(t);
}
t=(s*10+1)%n;
if(dp[t]==-1){
dp[t]=10*dp[s]+1;
que.push(t);
}
}
if(!fl)puts("-1");
}
return 0;
}

  

POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2的更多相关文章

  1. (简单) POJ 1426 Find The Multiple,BFS+同余。

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  2. POJ.1426 Find The Multiple (BFS)

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

  3. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

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

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

  5. POJ 1426 Find The Multiple(寻找倍数)

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

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

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

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

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

  8. POJ 1426 Find The Multiple(数论——中国同余定理)

    题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...

  9. POJ 1426 - Find The Multiple - [DP][BFS]

    题目链接:http://poj.org/problem?id=1426 Given a positive integer n, write a program to find out a nonzer ...

随机推荐

  1. Sum It Up---poj1564(dfs)

    题目链接:http://poj.org/problem?id=1564 给出m个数,求出和为n的组合方式:并按从大到小的顺序输出: 简单的dfs但是看了代码才会: #include <cstdi ...

  2. 洛谷P1858 多人背包 多人背包板子题/多人背包学习笔记

    ,,,本来自以为,我dp学得还挺好的 然后今天一考发现都不会啊QAQ 连最基础的知识点都不清楚啊QAQ 所以就来写个题解嘛! 先放下板子题 其实我jio得,这题只要大概了解方法就不是很难鸭,,,毕竟是 ...

  3. 【Python】Python 打印和输出更多用法。

    Python 打印和输出 简述 在编程实践中,print 的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用 print 来打印输出. 关于 print 函数,前面很多地方已经提及 ...

  4. React 教程

    React 入门实例教程 http://www.ruanyifeng.com/blog/2015/03/react.html React 测试入门教程http://www.ruanyifeng.com ...

  5. Linux下编译安装PHP扩展memcached

    [安装 libevent] $ tar zxvf libevent-2.0.20-stable.tar.gz $ cd libevent-2.0.20-stable/$ ./configure --p ...

  6. Django—Form两种保留用户提交数据的方法

    用户在网页上进行表单填写时,有可能出现某项填写错误.一般情况下,用户在未发觉错误的情况下点击提交,则此表单的内容会清空,用户不得不再重新填写,这样的用户体验是及其糟糕的. 在此,我们有2种方法将用户的 ...

  7. 运行.xcworkspace项目后报错:'React/RCTBundleURLProvider.h’ file not found

    情况:根据https://github.com/rebeccahughes/react-native-device-info添加依赖库,运行.xcworkspacea项目后报错 解决:Delete n ...

  8. springcloud9----feign-client-without-hystrix

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  9. 20145118 《Java程序设计》 第2周学习总结

    20145118 <Java程序设计> 第2周学习总结 教材学习内容总结 起初翻开课本看到第三章的章节题目”基础语法”时,我就明白这是一章需要我们牢牢掌握并理解的学科.通过看课本我了解到, ...

  10. Ubuntu 14.04安装gnuplot 解决Terminal type set to 'unknown'问题 简易命令教程

    参考: 照猫画虎学gnuplot之折线图 gnuplot 入门教程 1 gnuplot安装,及error:terminal type set to 'unknown'的解决 安装 sudo apt-g ...