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. rpc、socket、mq

    关于RPC与MQ异同的理解 相同:1.都利于大型系统的解耦:2.都提供子系统之间的交互,特别是异构子系统(如java\node等不同开发语言):不同:1.RPC侧重功能调用,因此多半是同步的:备注:也 ...

  2. XShell已经内置rz 直接从Windows拖文件进去终端

    XShell已经内置rz 直接从Windows拖文件进去终端 http://www.jb51.net/LINUXjishu/163820.html 借助securtCRT,使用linux命令sz可以很 ...

  3. [css]浮动-清除浮动的3种方法

    清除浮动的方法: 内墙法 注: 这是个奇淫技巧,没什么原理可言,记住即可 这个技巧又使得父box重新可以被子box撑开高度了. 隔墙法-适用于2个box之间上下排列 由于2个box高度依旧是0, 彼此 ...

  4. python实现文件夹遍历

    python 中os.path模块用于操作文件或文件夹 os.path.exists(path) 判断文件路径是否存在 dir = "c:\windows"if os.path.e ...

  5. 为什么要同时重写equals和hashcode

    原文地址https://blog.csdn.net/tiantiandjava/article/details/46988461 原文地址https://blog.csdn.net/lijiecao0 ...

  6. 图:无向图(Graph)基本方法及Dijkstra算法的实现 [Python]

    一般来讲,实现图的过程中需要有两个自定义的类进行支撑:顶点(Vertex)类,和图(Graph)类.按照这一架构,Vertex类至少需要包含名称(或者某个代号.数据)和邻接顶点两个参数,前者作为顶点的 ...

  7. Excel error 64-bit version of SSIS

    问题是 在windows server 2008 64位的计划任务执行 ssis 的错误 ,ssis你们带有读取excel 日期 2015/3/17 11:50:34日志 作业历史记录 (SSIS_U ...

  8. 使用老毛桃安装Windows操作系统

    首先必须知道什么是PE系统? 当电脑出现问题而不能正常进入系统时候的一种“紧急备用”系统,通常放在U盘中,设置启动项优先级,使得电脑启动的时候加载PE系统. 如何在U盘中安装老毛桃(PE工具箱)? h ...

  9. 【运维技术】node项目使用strongloop进行部署相关教程

    node项目使用strongloop进行部署相关教程 安装strongloop 下载安装node 解压到路径完成安装 使用软链方式配置环境变量 添加cnpm的淘宝镜像源 安装node-gyp的模块依赖 ...

  10. Sybase数据库常用sql语言

    Sybase数据库常用sql语言 1,表备份: --table_name1:需要备份的表; table_name2:备份后的表 SELECT * into table_name2 from table ...