POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
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的更多相关文章
- (简单) 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 ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- DFS/BFS(同余模) POJ 1426 Find The Multiple
题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...
- POJ 1426 Find The Multiple (DFS / BFS)
题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...
- POJ 1426 Find The Multiple(数论——中国同余定理)
题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...
- 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 ...
随机推荐
- 解决 Ubuntu 下 Sublime Text 无法输入中文的问题
解决 Ubuntu 下 Sublime Text 无法输入中文的问题 1. 安装依赖库 sudo apt-get install build-essential sudo apt-get instal ...
- Disruptor的伪共享解决方案
1.术语 术语 英文单词 描述 内存屏障 Memory Barriers 是一组处理器指令,用于实现对内存操作的顺序限制. In the Java Memory Model a volatile fi ...
- Linux下如何执行Shell脚本
Linux下你可以有两种方式执行Shell脚本: 1.用shell程序执行脚本:根据你的shell脚本的类型,选择shell程序,常用的有sh,bash,tcsh等(一般来说第一行#!/bin/bas ...
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求 ...
- python中的TCP及UDP
python中是通过套接字即socket来实现UDP及TCP通信的.有两种套接字面向连接的及无连接的,也就是TCP套接字及UDP套接字. TCP通信模型 创建TCP服务器 伪代码: ss = sock ...
- PR曲线 ROC曲线的 计算及绘制
在linear model中,我们对各个特征线性组合,得到linear score,然后确定一个threshold,linear score < threshold 判为负类,linear sc ...
- python webdriver 显示等待-自动登录126邮箱,添加联系人
脚本内容:#encoding=utf-8#author-夏晓旭from selenium import webdriverimport timefrom selenium.webdriver.supp ...
- 【开源】检查更新程序 CheckUpdate.Net 的实现
访问最新源代码及更新历史:http://git.oschina.net/xcong/CheckUpdate.Net DownLoad 更新历史 version 1.2 [新增]添加UpdateFile ...
- NOSQL数据库-Redis
官方提倡使用Linux版的Redis,所以官网值提供了Linux版的Redis下载,我们可以从GitHub上下载window版的Redis,具体链接地址如下: · 官网下载地址:http://redi ...
- pyDay13
内容来自廖雪峰的官方网站. 1.把list.dict.str等Iterable变成Iterator可以使用iter()函数 >>> L = iter([1, 2, 3, 4, 5, ...