POJ 1426 Find The Multiple
POJ 1426 Find The Multiple
题目链接http://poj.org/problem?id=1426
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.
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
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
题意:
给你一个数字n然后找出由0,1组成的数字可以整除这个数字。
题解:
可以使用查找,首先第一位一定是1.然后对于后面有两种可能,一种是加0,一种是加1.可以使用队列,同时不断取余。对于一个数字来说(ab)%mod == (a%mod)(b%mod);(a+b)%mod == (a%mod) + (b%mod);
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
long long solve(int n)
{
queue <long long> qu;
qu.push(1);
long long temp;
while (!qu.empty()){
temp = qu.front();
qu.pop();
if ((temp*10)%n == 0)
return temp*10;
if ((temp*10+1)%n == 0)
return temp*10+1;
qu.push(temp*10);
qu.push(temp*10+1);
}
return -1;
}
int main(int argc, char const *argv[])
{
int n;
while (scanf("%d",&n) && n){
long long ans = solve(n);
cout<<ans<<endl;
}
return 0;
}