[kuangbin带你飞]专题一 简单搜索E

来源:互联网 发布:恶搞新闻联播软件 编辑:程序博客网 时间:2024/06/06 09:38

E - Find The Multiple

 

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.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
26190
Sample Output
10100100100100100100111111111111111111

题意:给你一个数n让你找一个只有0和1组成的数可以被n整除。

思路:简单深搜,直接每次*10或者*10+1就行;有两个坑点:1.Special judge YES。2.因为数会很大,超过long long范围(正负9223372036854775807)的时候就直接return ;

#include<iostream>#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n;bool flag;void dfs(int k,long long int sum)//k为这个数的位数,当这个数的位数超过long long范围(19位)就停止 {if(k==20||flag==true){return ;}if(sum%n==0){flag=true;cout<<sum<<endl;return ;}dfs(k+1,sum*10);dfs(k+1,sum*10+1);}int main(){while(cin>>n&&n){flag=false;dfs(1,1);}}


0 0
原创粉丝点击