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. Tickets---hdu1260 (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 题意就是有n个人排队买票,每个人需要的时间是a[i] (1=< i <=N),但是现 ...

  2. 理解Java异常处理机制的机理

    重看异常机制的时候觉得抓到了点机理上的精髓,所以来说一下,对初学者应该会有些帮助   JAVA中的异常机制 从机制上由[产生异常][抛出异常][捕捉异常][异常处理]组成 从形式上又分为四种: 运行时 ...

  3. 符合语言习惯的 Python 优雅编程技巧

    Python最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净.整洁.一目了然.要写出 Pythonic(优雅的.地道的.整洁的)代码,需要多看多学大牛们写的代码,github 上有很多非常优秀 ...

  4. .NET、NET Framewor以及.NET Core的关系(一)

    什么是.NET?什么是.NET Framework?本文将从上往下,循序渐进的介绍一系列相关.NET的概念,先从类型系统开始讲起,我将通过跨语言操作这个例子来逐渐引入一系列.NET的相关概念,这主要包 ...

  5. 通过生成器yield实现单线程的情况下实现并发运算效果(异步IO的雏形)

    一.协程: 1.生成器只有在调用时才会生成相应的数据 2.调用方式有 " str__next__.()   str.send() ", 3.并且每调用一次就产生一个值调用到最后一个 ...

  6. JS获取客户端系统当前时区

    <script> function getClientTimezone(){ var oDate = new Date(); var nTimezone = -oDate.getTimez ...

  7. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

  8. hdu1700 Points on Cycle

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1700 题目: Points on Cycle Time Limit: 1000/1000 MS ...

  9. myeclips破解

    MyEclipse官方安装文件,下载地址 http://www.jb51.net/softs/150886.html破解补丁http://www.jb51.net/softs/150887.html ...

  10. pyDay12

    内容来自廖雪峰的官方网站. 1.可迭代对象(Iterable):可以直接作用于for循环的对象. 2.集合数据类型:如list.tuple.dict.set.str等. 3.generator:包括生 ...