51NOD 1109 01组成的N的倍数

7 篇文章 0 订阅
4 篇文章 0 订阅
1109 01组成的N的倍数
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏  关注
给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含01。求最小的M。
例如:N = 4,M = 100Input
输入1个数N。(1 <= N <= 10^6)
Output
输出符合条件的最小的M。
Input示例
4
Output示例
100
相关问题
01组成的N的倍数 V2 320
李陶冶 (题目提供者)

首先 很容易得到一个 暴力枚举 模拟M%N的算法

队列里放入 M=1
if (M%N!=0):
    队列里再放入M*10 , M*10+1
else :
    M就是答案    

显然会超时
不难发现 如果 M’=M / 10^t
M’%N = M%N != 0 那M’没必要放进队列里了 肯定不能得到最小值
so:加入个used数组记录余数k是否出现过 居然卡着1000ms过了….


//string暴力 1000ms
const int N = 1e6+10;

bool used[N];

int check(const string&m,const int n){
    int t=0;
    for(int i=0;i<m.size();++i){
        t=(m[i]-'0'+t*10)%n;
    }
    return t;
}

void slove(int n){
    fill(used,used+n+1,false);
    deque<string>que;
    que.push_back("1");
    for(;;){
        string m=que.front();
        que.pop_front();
        int chAns = check(m,n);
        if(chAns==0){
            cout<<m<<endl;
            break;
        }
        else{
            if(used[chAns]==false){
                que.push_back(m+'0');
                que.push_back(m+'1');
                used[chAns]=true;
            }
        }
    }
}

根据这个余数 再进一步优化
m' = m*10 +k | k=0,1
pre[i] = m' % n
pre[ pre[i] ] = m%n
val[i] = 余数为 i 时 对应的 m%10

用来代替string 优化到46ms


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 1e6+10;

int pre[N];//pre[i]=余数为i时 上一次余数是多少
int val[N];//val[i]=余数为i时 xx1%n = i val[i]=1  xxx0%n = i val[i]=0

void print(int x){
    if(pre[x]!=-1){
        print(pre[x]);
    }
    printf("%d",val[x]);
}

void bfs(int n){
    deque<int>que;
    que.push_back(1);
    pre[1]=-1;
    val[1]=1;
    while(!que.empty()){
        int x=que.front();
        que.pop_front();
        int x1=x*10,x2=x1+1;
        x1%=n;
        x2%=n;
        if(0==pre[x1]){//余数为x1时
            pre[x1]=x;
            val[x1]=0;
            que.push_back(x1);
        }
        if(0==pre[x2]){
            val[x2]=1;
            pre[x2]=x;
            que.push_back(x2);
        }
        if(0==x1){
            print(x1);
            break;
        }
        if(0==x2){
            print(x2);
            break;
        }
    }
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int n;
    scanf("%d",&n);
    if(n==1){
        printf("1\n");
    }
    else{
        bfs(n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值