[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题,没有太多好说的,代码写的效 ...
随机推荐
- JavaScript设计模式之单例模式【惰性单例】
在提高开发水平,往中高级前端工程师中,利用设计模式是必不可少的一条道路.掌握设计模式的思想远远比硬套重要,因为设计模式是一种思想,不局限于开发语言.但实际上由于语言的特性不同,往往在实现的时候会有不少 ...
- Petya and Graph/最大权闭合子图、最小割
原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...
- 4 IDEA环境应用
第4章 IDEA环境应用 spark shell仅在测试和验证我们的程序时使用的较多,在生产环境中,通常会在IDE中编制程序,然后打成jar包,然后提交到集群,最常用的是创建一个Maven项目,利用M ...
- nova start 虚机的代码流程分析
nova start 虚机的代码流程分析,以ocata版本为分析基础1.nova api服务接受用户下发的 nova start启动虚机请求其对应的http restfull api接口为post / ...
- SpringBoot集成Swagger2,3分钟轻松入手!
一.引入maven <dependency> <groupId>io.springfox</groupId> <artifactId>springfox ...
- 牛客网PAT练兵场-程序运行时间
题解:无(注意下四舍五入和输出格式即可) 题目地址:https://www.nowcoder.com/questionTerminal/fabbb85fc2d342418bcacdf0148b6130 ...
- 【小白学AI】GBDT梯度提升详解
文章来自微信公众号:[机器学习炼丹术] 文章目录: 目录 0 前言 1 基本概念 2 梯度 or 残差 ? 3 残差过于敏感 4 两个基模型的问题 0 前言 先缕一缕几个关系: GBDT是gradie ...
- Flyway版本化管理数据库脚本
假如我们有一个叫shiny的项目,它是由一个程序Shiny-Server 和一个数据库 Shiny-DB组成的; 简单结构图如下: 但是很多时候,现实开发团队是这样的: 我们的项目shiny项目的运行 ...
- 分分钟玩转UI自动化测试
有没有那么一刻,看到自动模拟用户操作界面感觉好神奇. 关于什么叫 UI 自动化测试就不解释了,基本上是你刚才脑海里想到什么就是什么. 在分层自动化测试中包括:UI 测试.集成/接口测试.单元测试.大神 ...
- windows 下编译libcurl
因为linux平台采用了libcurl,有一个程序移植到到windows平台,再linux采用libcurl.在windows下准备也采用该库.在网上搜索了几位同行写的,步骤上面有缺失. 本文将以详细 ...