[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);
}
}
posted on 2019-05-28 23:56 coding_gaga 阅读(174) 评论(0) 编辑 收藏 举报