Description

  Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
 
  大意就是说求一个只有0,1的数,是n的倍数。
  其实是一个水题,第一遍做的时候直接枚举1,10,11,100,101,110,111...一直这样下去,然后就过了。。。。后来看了一下题解,说可以用BFS+同余来做,然后就试了一下,时间减去了不少。
  而且这个题目对于n是偶数或者n是5的倍数可以直接求出n/2或n/5的来然后加个0就好了。。。
 
这是第一遍的代码:
#include<iostream>
#include<cstring> using namespace std; long long ans[]; bool panduan(int a,int b)
{
long long num=;
long long base=; while(b)
{
if(b%)
num+=base;
base*=;
b/=;
} if(num%a==)
{
ans[a]=num;
return ;
} return ;
} int main()
{
int cou=;
for(int i=;i<=;i+=)
if(i%)
{
for(int j=;j<(<<);++j)
if(panduan(i,j))
{
break;
}
} int k,rem; ios::sync_with_stdio(false); for(cin>>k;k;cin>>k)
{
rem=;
while(k%==)
{
k/=;
++rem;
}
while(k%==)
{
k/=;
++rem;
}
cout<<ans[k];
for(int i=;i<rem;++i)
cout<<;
cout<<endl;
} return ;
}

这是第二遍的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<utility> using namespace std; int que[],las,fir; void showans(int x)
{
int ans[];
int cou=; while(x)
{
if(x&)
ans[cou++]=;
else
ans[cou++]=; x=x>>;
} for(int i=cou-;i>=;--i)
cout<<ans[i]; cout<<endl;
} inline void getans(int n)
{
las=fir=; int cou=;
int temp; que[las++]=; while(las-fir)
{
++cou;
temp=que[fir++]; if(!temp)
{
showans(cou);
return;
} que[las++]=(temp*)%n;
que[las++]=(temp*+)%n;
}
} int main()
{
ios::sync_with_stdio(false); int n; for(cin>>n;n;cin>>n)
{
getans(n);
} return ;
}

(简单) POJ 1426 Find The Multiple,BFS+同余。的更多相关文章

  1. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  2. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

  3. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

  4. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

  5. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  6. DFS/BFS(同余模) POJ 1426 Find The Multiple

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

  7. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  8. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  9. POJ 1426 Find The Multiple (DFS / BFS)

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

随机推荐

  1. 获取sql执行时间

    sql server中获取要执行的sql或sql块的执行时间,方法之一如下: declare @begin datetime,@end datetime set @begin =getdate() - ...

  2. table详解

    1.tr 元素定义表格行,th 元素定义表头,td 元素定义表格单元. tr内是th还是td可由自己定义,th,td可存在于任一行,th与td的区别在与th字体更粗 2.定义一个table默认有bor ...

  3. iOS开发工具——统计Crash的工具Crashlytics

    简介 Crashlytic 成立于2011年,是专门为移动应用开者发提供的保存和分析应用崩溃信息的工具.Crashlytics的使用者包括:支付工具Paypal, 点评应用Yelp, 照片分享应用Pa ...

  4. PAT乙1001

    我脑洞大了..... 以为是个找规律..... 原来只是模拟..... 我相信肯定是有规律的..... 但是我就是不愿意了..... #include<cstdio> #include&l ...

  5. ZooKeeper概述

    1.Zookeeper概述 Zookeeper 是 Google 的 Chubby一个开源的实现,是 Hadoop 的分布式协调服务.它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置 ...

  6. SecureCRT 上传文件的两种方法 Zmodem、SFTP

    Zmodem: 无论有xshell还是secureCRT连接linux的时. 默认都用一个zmodem可以帮助window和linux之间传输文件 很方便和实用的工具. 不过默认是无法使用的 需要安装 ...

  7. android使用百度app分享,app统计出现的异常,FrontiaApplication类

    想在app里加入百度分享和百度统计.查看了百度移动统计的文档后下载官网给的demo.参照demo给出的代码给自己的app加入代码.以上步骤比较简单.不细说,下面说下此过程需要注意的一些点,不然会引发异 ...

  8. MDK下调试时提示AXF文件无法导入的解决方法(转)

    源:http://blog.163.com/zhaojun_xf/blog/static/3005058020117784643555/ 在开发ARM项目中,很多工程师都不太喜欢使用MDK的调试模式, ...

  9. FusionCharts使用问题及解决方法(四)-FusionCharts常见问题大全

    在前3篇文章中,我们总结了FusionCharts图表的一些常见问题(FAQ)及解决方法,本文继续讨论FusionCharts使用者常见的一些复杂的报错及解决方法. 问题描述:使用FusionChar ...

  10. HDU 2612 Find a way BFS,防止超时是关键

    之前我写的时候是:每找到一个‘@’就广搜一次,如果这样写有多少个‘@’就会广搜几次,这样就超时了.我队友告诉我应该打个表,这个方法确实不错.因为'Y'和'M'是唯一的,我通过这两个点分别广搜一次,对所 ...