搜索的好题,,,,

摘自题解:

题意;

给一个数n,求n 的最小的倍数,满足它的10进制 表示中每一位不是0就是1。

思路:

用f(x)表示被n整除取模后的最小数,那么从0开始,每次往后添0或者1,如果得到的数与某个已经得到的数同余,就扔掉,不然就加入队列中继续搜。。。

时间复杂度O(N).

代码如下:

#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
#define M 20005
struct Node{
int m;
bool flag;
};
bool vis[M];
Node a[M];
int ok(int x)
{
int t;
while(x)
{
t = x%10;
x/=10;
if(t!=0&&t!=1) return 0;
}
return 1;
}
void print(int x)
{
if(x<0) return;
print(a[x].m);
printf("%d", a[x].flag);
if(x==0) printf("\n");
}
int main ()
{
int t, n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(vis,0,sizeof(vis));
if(ok(n)) printf("%d\n", n);
else
{
queue<int>q;
int tt1, tt2;
vis[1] = 1;
a[1].m = -1;
a[1].flag = 1;
q.push(1);
while(!q.empty())
{
tt1 = q.front();
q.pop();
if(vis[(tt1*10)%n]==0)
{
tt2 = tt1*10%n;
a[tt2].m = tt1;
a[tt2].flag = 0;
if(tt2==0) break;
vis[tt2] = 1;
q.push(tt2);
}
if(vis[(tt1*10+1)%n]==0)
{
tt2 = (tt1*10+1)%n;
a[tt2].m = tt1;
a[tt2].flag = 1;
if(tt2==0) break;
vis[tt2] = 1;
q.push(tt2);
}
}
print(0);
}
}
return 0;
}

SPOJ ONEZERO(搜索)的更多相关文章

  1. 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)

    [BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...

  2. SPOJ:Strange Waca(不错的搜索&贪心&剪枝)

    Waca loves maths,.. a lot. He always think that 1 is an unique number. After playing in hours, Waca ...

  3. SPOJ Hacking(字典树 + 搜索)题解

    思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...

  4. ACM 暴力搜索题 题目整理

    UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...

  5. 【SPOJ】【1825】Free Tour 2

    点分治 点分治的例题2(本题代码结果为TLE……) 强烈谴责卡时限QAQ,T了无数次啊无数次…… 不过在N次的静态查错中倒是加深了对点分治的理解……也算因祸得福吧(自我安慰一下) TLE后的改进:每棵 ...

  6. SPOJ KPSUM ★(数位DP)

    题意 将1~N(1<=N<=10^15)写在纸上,然后在相邻的数字间交替插入+和-,求最后的结果.例如当N为12时,答案为:+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2=5 ...

  7. CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)

    题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...

  8. Meet in the middle算法总结 (附模板及SPOJ ABCDEF、BZOJ4800、POJ 1186、BZOJ 2679 题解)

    目录 Meet in the Middle 总结 1.算法模型 1.1 Meet in the Middle算法的适用范围 1.2Meet in the Middle的基本思想 1.3Meet in ...

  9. 线性dp(记忆化搜索)——cf953C(经典好题dag和dp结合)

    非常好的题!和spoj 的 Mobile Service有点相似,用记忆化搜索很容易解决 看了网上的题解,也是减掉一维,刚好可以开下数组 https://blog.lucien.ink/archive ...

随机推荐

  1. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  2. Dig out deleted chat messages of App Skype

    Last month Candy was arrested on suspicion of having doing online porn webcam shows, but Candy refus ...

  3. Android IOS WebRTC 音视频开发总结(七二)-- 看到Google Duo,你想到了什么?

    本文主要介绍在线教育这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 在昨天的Google I/O大会上Go ...

  4. 【EF学习笔记09】----------使用 EntityState 枚举标记实体状态,实现增删改查

    讲解之前,先来看一下我们的数据库结构:班级表 学生表 如上图,实体状态由EntityState枚举定义:Detached(未跟踪).Unchanged(未改变).Added(已添加).Deleted( ...

  5. centos6.5环境源码编译安装mysql5.6.34

    centos6.5环境源码编译安装mysql5.6.34 源码下载地址http://dev.mysql.com/downloads/mysql/5.6.html#downloads 选择Generic ...

  6. 4.Mybatis的输入映射(parameterType类型解析)

    前面提到过Mybatis可以对输入的参数进行映射,那么现在我们来看一下输入映射,关于输入映射大概可以分为几种情况来学习: 1.基本的类型 2.实体类 3.包装类 1.参数是基本的类型(int,Stri ...

  7. 提示框(UIAlertController)的使用。

    添加出现在屏幕中间的提示框(也就是之前的UIAlertView): UIAlertController * av = [UIAlertController alertControllerWithTit ...

  8. jQuery.bind() 函数详解

    bind()函数用于为每个匹配元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 执行bind()时,事件处理函数会绑定到每个匹配元素上.因此你使用bind( ...

  9. performSelector may cause a leak because its selector is unknown解决

    解决方法 SEL selector = NSSelectorFromString(@"applySketchFilter:"); IMP imp = [FWApplyFilter ...

  10. oracle备忘

    一.explain plan a)explain plan for select ... select * from table(dbms_xplan.display()); function dis ...