Multiples of N composed of 51Nod 1109 01

Given a natural number N, find an M such that M > 0 and M is a multiple of N, and the decimal representation of M contains only 0 or 1. Find the smallest M.

 
For example: N=4, M=100.
Input
Enter a number N. (1 <= N <= 10^6)
Output
Output the smallest M that meets the conditions.
Input example
4
Output example
100 
data may be very large, so long long will burst, so the remainder can be handled, and the remainder cannot exceed 999999.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
int n;
bool vis[1000006];
struct node
{
    string str;
    int data;
}years,pos;
void dfs(int n)
{
    queue<node>q;
    memset(vis,0,sizeof(vis));
    ans.str="1";
    ans.data = 1 % n;
    vis[ 1 %n]= 1 ;
    q.push(ans);
    while(!q.empty())
    {
        pos=q.front();
        q.pop();
        if(pos.data%n==0)
        {
            cout<<pos.str<<endl;
            return ;
        }
        for(int i=0;i<2;i++)
        {
            if(!vis[(pos.data*10+i)%n])
            {
                ans.str = pos.str;
                ans.str+=i+'0';
                ans.data=(pos.data*10+i)%n;
                vis [ans.data] = 1 ;
                q.push(ans);
            }
        }
    }
}
intmain ()
{
    scanf("%d",&n);
    dfs(n);
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325138363&siteId=291194637