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 ...
随机推荐
- Scala简介及基础语法
一.scala简介 官网:https://www.scala-lang.org/ Scala语言很强大,集成了面向对象和函数式编程的特点. 运行在JVM(jdk). 大数据中为什么学习scala? s ...
- BaseLayout
angularjs2 knockoutjs framework7 jquerymobile bootstrap html5 css [Activity(Label = "ActivityBa ...
- PAT 1060 Are They Equal[难][科学记数法]
1060 Are They Equal(25 分) If a machine can save only 3 significant digits, the float numbers 12300 a ...
- docker——安全防护与配置
Docker是基于Linux操作系统实现的应用虚拟化.运行在容器内的进程,跟运行在本地系统的进程本质上并无区别,配置不合适的安全策略将可能给本地系统带来安全风险,因此,Docker的安全性在生产环境中 ...
- 1.1 Getting Started-Core Concepts
一.Templates 使用Handlebars模板语言来描述程序的用户接口.每一个模板都有model的支持,如果model改变template就会自动更新. Expressions: li ...
- 1.2 Getting Started--Naming Conventions(命名约定)
Ember.js使用一个运行时解析器去连接你的对象而没有很多样板文件.作为一个开发者,如果你把code放到约定好的位置这个解析器会自动工作. 一.The Application 1. 当你 ...
- 2018 Multi-University Training Contest 7 Solution
A - Age of Moyu 题意:给出一张图,从1走到n,如果相邻两次走的边的权值不同,花费+1, 否则花费相同,求最小花费 思路:用set记录有当前点的最小花费有多少种方案到达,然后最短路 #i ...
- oracle_多字段统计(多count)
oracle_多字段统计 查询同一张表中同一字段的不同值的综合,方法如下: select o.code 礼品代码, o.name 礼品名称, l.couponactivityid 券活动定义, cou ...
- 聊一聊PV和并发、以及计算web服务器的数量的方法(转)
转自:http://www.chinaz.com/web/2016/0817/567752.shtml 最近和几个朋友,聊到并发和服务器的压力问题.很多朋友,不知道该怎么去计算并发?部署多少台服务器才 ...
- Ubuntu下常用强化学习实验环境搭建(MuJoCo, OpenAI Gym, rllab, DeepMind Lab, TORCS, PySC2)
http://lib.csdn.net/article/aimachinelearning/68113 原文地址:http://blog.csdn.net/jinzhuojun/article/det ...