(简单) POJ 1426 Find The Multiple,BFS+同余。

时间:2021-09-12 03:43:46

  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 &vert;&vert; DFS

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

  2. poj 1426 Find The Multiple &lpar;bfs 搜索)

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

  3. POJ 1426&Tab;Find The Multiple BFS

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

  4. poj 1426 Find The Multiple &lpar; BFS&plus;同余模定理&rpar;

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

  5. POJ&period;1426 Find The Multiple (BFS)

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

  6. DFS&sol;BFS&lpar;同余模&rpar; POJ 1426 Find The Multiple

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

  7. 广搜&plus;打表 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 &sol; BFS)

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

随机推荐

  1. 1Z0-053 争议题目解析606

    1Z0-053 争议题目解析606 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 606.Identify the channel settings that can be per ...

  2. Python-8 元组tuple

    #1 特殊的列表:元组 元组中的元素不可改变 #2 创建.访问 >>> tuple1=(1,2,3) >>> tuple1=1,2,3 >>> t ...

  3. 使用CTE解决复杂查询的问题

    最近,同事需要从数个表中查询用户的业务和报告数据,写了一个SQL语句,查询比较慢: Select S.Name, S.AccountantCode, ( Select COUNT(*) from ( ...

  4. Android复习笔记--Activity

    #Activity注册 Android四大组件(Activity,Service,Broadcast Receiver, Content Provider)都需要在AndroidManifest中注册 ...

  5. 用Mysqlbinlog备份BinLog文件

    默认情况下, mysqlbinlog读取二进制文件[BinLog]并以文本的方式呈现[text format].mysqlbinlog可以直接地从本地读取Log,也可以读取远程的Log[--read- ...

  6. js&colon;数据结构笔记10--图和图算法

    图:是由边和定点的集合组成:  按照图的定点对是否有序可以分为:有向图和无向图:  路径:所有顶点都由边连接构成:路径长度为第一个定点到最后一个顶点之间的数量:  环:指向自身的顶点,长度为0:圈:至 ...

  7. 如何使用JCA &lpar;J2EE 连接器架构)实现企业应用--转载

    JCA (J2EE 连接器架构,Java Connector Architecture)是对J2EE标准集的重要补充.因为它注重的是将Java程序连接到非Java程序和软件包中间件的开发.连接器特指基 ...

  8. linux&colon; 几个常用makefile模板

    不才,总结个人常用makefile模板,以备后用. 1.编译动态库 ############################################################# # Ma ...

  9. Android studio 中添加依赖model时依赖所需的准备

    例如向app中添加依赖core: core要做如下修改: 1.将core中build.gradle中   修改为  . 2.将core中的 applicationId 注释掉.

  10. PHP通过循环给数组赋值

    $userDatas = [ ['user_id'=>1], ['user_id'=>3] ]; $userIds = []; foreach ($userDatas as $item){ ...