[POJ]Find The Multiple(DFS)
题目链接
http://poj.org/problem?id=1426
题意
输入一个数n,输出任意一个 只含0、1且能被n整除的数m。保证n<=200,m最多100位。
题解
DFS/BFS都能做。
这里使用DFS。然后T了。就变成了打表A了。emm ,m最多会19位,这点我也很迷==。
其他
使用了Java大数,get了打开方式。
todo
- 为啥m19??
- BFS法
- 0/1背包法
- 和鸽笼原理有什么关系?
代码
打表A版
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static int num;
static boolean tag;
static BigInteger[] arr=new BigInteger[201];
static String[] ans= {"0","1","10","1000000000000000011","100","10","1000000000000000110","1000000000000000111","1000","1000000000011111111","10","100000000000000001","1000000000000001100","1000000000000001","10000000000000010","1000000000000000110","10000","10000000000000101","1000000000111111110","1000000000000001101","100","10000000000000101","1000000000000000010","1000000000000010111","1000000000000011000","100","10000000000000010","1000000000101111111","100000000000000100","1000000000000000111","1000000000000000110","1000000000000110011","100000","1000000000000101111","100000000000001010","10000000000000010","1000000001111111100","100000000000000011","10000000000000110","10000000000000101","1000","1000000000000010111","100000000000001010","1000000000000010101","100000000000000100","1000000000111111110","100000000000001110","1000000000000101","1000000000000110000","1000000000000011101","100","10000000000000101","100000000000000100","1000000000000000011","1000000001101111110","1000000000000000010","1000000000000001000","1000000000000011","1000000000000001010","10000000000000011","1000000000000001100","1000000000000011","100000000001110010","1000000001011101111","1000000","10000000000000010","1000000000000111110","100000000000011","1000000000000010100","100000000000011","10000000000000010","1000000000000100101","1000000011111111000","1000000000001","1000000000000000110","1000000000000001100","100000000000001100","1000000000000001","100000000000001010","100000000000110001","10000","1000000000111111101","1000000000000110110","100000000000001101","1000000000000010100","100000000000001010","100000000000010010","100000000000000101","1000000000000001000","1000000000000011011","1000000000111111110","1000000000000001","1000000000000011100","1000000000001111001","10000000000001010","10000000000000110","1000000000001100000","100000000001111","1000000000000111110","1101111111111111111","100","1000000000000000001","100000000000001010","100000000000000001","1000000000000001000","100000000000001010","100000000010000110","10000000000001001","1000000001111111100","1000000000001000011","1000000000000000010","100000000000000011","100000000000110000","1000000000000111101","10000000000000110","100000000000001110","10000000000000100","1000000001101111101","100000000000000110","10000000000000101","1000000000000011000","1000000000000101111","10000000000000110","1000000001100011001","1000000000011100100","1000","1000000001111011110","1000000000000110101","10000000","10000000000001001","10000000000000010","10000000001101","1000000000010111100","1000000000001010001","1000000000000110","1000000001101111110","100000000000111000","1000000000001","1000000000000110","1000000000001001111","100000000000000100","1000000000000101","1000000000011110","1000000000000001","1000000111111110000","1000000000000001010","10000000000010","100000000000011111","1000000000000010100","10000000000001111","1000000000000001100","1000000000010111001","1000000000000011000","1000000000111111101","10000000000000010","100000000001110010","1000000000000010100","1000000000000101","1000000000001100010","1000000000000000011","100000","1000000000001101001","100000000111111110","1000000000011011101","1000000000001110100","1000000000000111110","1000000000000011010","1000000000001001101","10000000000011000","1000000000001100111","100000000000001010","1000000000111111011","1000000000000100100","1000000000000010111","1000000000000001010","100000000000000100","100000000000010000","10000000000000011","100000000000001110","1000000000000100111","1000000001111111100","10000000000000111","10000000000000010","1000000000000011","100000000000011000","1000000000000000110","10000000001101110","100000000001001001","100000000000010100","1000000010111010111","10000000000000110","1000000000011111101","1000000000011000000","1000000010000100001","1000000000011110","100000000000001010","100000000010010100","100000000000111001","1111111111111111110","1000000000101111001","1000"};
public static void main(String args[]) {
// //打表
// for(int i=1;i<201;++i) {
// tag=false;
// dfs(BigInteger.valueOf(1),i,1);
// System.out.print("\"");
// System.out.print(arr[i]);
// System.out.print("\"");
// System.out.print(",");
// }
Scanner in=new Scanner(System.in);
while(in.hasNext()) {
int num=in.nextInt();
if(num==0) {break;}
System.out.println(ans[num]);
}
}
public static void dfs(BigInteger x,int num,int layer) {
if(tag) {return;}//
if(x.mod(BigInteger.valueOf(num))==BigInteger.ZERO) {
//System.out.println(x);
arr[num]=x;
tag=!tag;
return;
}
if(layer==19) {return;}
dfs(x.multiply(BigInteger.valueOf(10)),num,layer+1);
dfs(x.multiply(BigInteger.valueOf(10)).add(BigInteger.ONE),num,layer+1);
}
}
DFS T版,估计是JAVA大数T的?C++用unsigned long long这么写感觉不会T?
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static int num;
static boolean tag;
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
while(in.hasNext()) {
int num=in.nextInt();
if(num==0) {break;}
tag=false;
dfs(BigInteger.valueOf(1),num,1);
}
}
public static void dfs(BigInteger x,int num,int layer) {
if(tag) {return;}//
if(x.mod(BigInteger.valueOf(num))==BigInteger.ZERO&&!tag) {
System.out.println(x);
tag=!tag;
return;
}
if(layer==19) {return;}
dfs(x.multiply(BigInteger.valueOf(10)),num,layer+1);
dfs(x.multiply(BigInteger.valueOf(10)).add(BigInteger.ONE),num,layer+1);
}
}
[POJ]Find The Multiple(DFS)的更多相关文章
- POJ 1321 棋盘问题 --- DFS
POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- POJ 1655 - Balancing Act - [DFS][树的重心]
链接:http://poj.org/problem?id=1655 Time Limit: 1000MS Memory Limit: 65536K Description Consider a tre ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- 开篇,UVA 755 && POJ 1002 487--3279 (Trie + DFS / sort)
博客第一篇写在11月1号,果然die die die die die alone~ 一道不太难的题,白书里被放到排序这一节,半年前用快排A过一次,但是现在做的时候发现可以用字典树加深搜,于是乐呵呵的开 ...
- poj 1416 Shredding Company( dfs )
我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- POJ 1699 Best Sequence dfs
题目: http://poj.org/problem?id=1699 无意间A了..超时一次,加了一句 if(len > ans)return; 然后就A了,dfs题,没有太多好说的,代码写的效 ...
随机推荐
- MySQL元数据信息
一.查看数据库清单 show databases; 二.查看数据库下表清单 方法1) show full tables from ${schema名}; 方法2) use ${数据库名}; show ...
- 精讲RestTemplate第7篇-自定义请求失败异常处理
本文是精讲RestTemplate第7篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- 关于MapReduce默认分区策略
MapReduce默认分区策略 mapreduce 默认的分区方式是hashPartition,在这种分区方式下,KV对根据key的hashcode值与reduceTask个数进行取模,决定该键值对该 ...
- Linux 将文件打包、压缩并分割成指定大小
打包文件: tar -cvf .tar 分割文件: split -b 3G -d -a .tar .tar. //使用split命令,-b 3G 表示设置每个分割包的大小,单位还是可以k // -d ...
- 学习一下 JVM (二) -- 学习一下 JVM 中对象、String 相关知识
一.JDK 8 版本下 JVM 对象的分配.布局.访问(简单了解下) 1.对象的创建过程 (1)前言 Java 是一门面向对象的编程语言,程序运行过程中在任意时刻都可能有对象被创建.开发中常用 new ...
- Hadoop 3.x 与Hadoop 2.x 的区别和优化点
Hadoop 3.x 与Hadoop 2.x 的区别和优化点 通用性 1.精简Hadoop内核,包括剔除过期的API和实现,将默认组件实现替换成最高效的实现(比如将FileOutputCommitte ...
- Git-commit-中添加表情
git commit 中使用表情 我们经常可以在github上看到国外大佬的commit信息中有很多可爱的表情,这是怎么做到的呢? ok,可以这样使用哦:git commit -m '提交信息 :em ...
- Java面试题(Java基础篇)
Java 基础 1.JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java Run ...
- oracle impdp 数据迁移 至RDS 亚马逊云
背景: 公司年底打算将aws rds11.2.0.4 oracle 数据库升级到19c,所以需要进行升级测试,所以需要我把线上的库数据迁移到一台测试的rds oracle 亚马逊云的数据库中,然后升级 ...
- Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸
前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 音频解析方法: public static int toInt(byte[] b) { return ((b[3] << 2 ...