51Nod 1109 01组成的N的倍数

给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含0或1。求最小的M。

 
例如:N = 4,M = 100。
Input
输入1个数N。(1 <= N <= 10^6)
Output
输出符合条件的最小的M。
Input示例
4
Output示例
100
数据可能非常大,因此long long会爆,因此可以处理余数,余数最大不超过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;
}ans,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);
            }
        }
    }
}
int main()
{
    scanf("%d",&n);
    dfs(n);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8979851.html