SPOJ ONEZERO(搜索)
搜索的好题,,,,
摘自题解:
题意;
给一个数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(搜索)的更多相关文章
- 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)
[BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...
- SPOJ:Strange Waca(不错的搜索&贪心&剪枝)
Waca loves maths,.. a lot. He always think that 1 is an unique number. After playing in hours, Waca ...
- SPOJ Hacking(字典树 + 搜索)题解
思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- 【SPOJ】【1825】Free Tour 2
点分治 点分治的例题2(本题代码结果为TLE……) 强烈谴责卡时限QAQ,T了无数次啊无数次…… 不过在N次的静态查错中倒是加深了对点分治的理解……也算因祸得福吧(自我安慰一下) TLE后的改进:每棵 ...
- 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 ...
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
- 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 ...
- 线性dp(记忆化搜索)——cf953C(经典好题dag和dp结合)
非常好的题!和spoj 的 Mobile Service有点相似,用记忆化搜索很容易解决 看了网上的题解,也是减掉一维,刚好可以开下数组 https://blog.lucien.ink/archive ...
随机推荐
- JS实现转动效果
方案一 <div class="div_uploading"> <div class="div_uploading_scroll">&l ...
- 纸上谈兵:队列(queue)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 队列(queue)是一个简单而常见的数据结构.队列也是有序的元素集合.队列最大的特 ...
- apache+tomcat分布式集群搭建
今天搭建apche+tomcat分布式集群,遇到很多问题,在网上找到的很多都不成功,然后和同事一起研究了一下,最终搭建成功了.做个笔记,以备自己以后参考. 1,下载apache.在下载Apache(2 ...
- delphi ftBlob二进制字段读取存储
aStream:TMemoryStream; 保存到字段 qrypub.ParamByName('Data').LoadFromStream(aStream, ftBlob); 从字段读取到mem里 ...
- Lock锁的使用示例
Lock锁是java5用来代替synchronized的一种面向对象的锁的方案 public class LockDemo { /** * Lock是用来替换synchronized, 优点是Lock ...
- 优雅的设计单线程范围内的数据共享(ThreadLocal)
单线程范围内数据共享使用ThreadLocal /** * @Description TODO * @author zhanghw@chinatelecom.cn * @since 2015年12月1 ...
- win 7 IIS 配置
http://jingyan.baidu.com/article/219f4bf723bcb2de442d38ed.html win7旗舰版系统 点击开始→控制面板,然后再点击程序,勿点击卸载程序,否 ...
- jquery table 拼接集合
1html: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- ide编辑器
http://wowubuntu.com/markdown/#editor https://netbeans.org/downloads/start.html?platform=windows& ...
- 定位一组对象-checkbox 、radiobutton
webdriver 可以很方便的使用find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法find_element ...