zoukankan      html  css  js  c++  java
  • TOJ3031: Multiple bfs

    3031: Multiple 

    Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte
    Total Submit: 60            Accepted:10

    Description

    a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).

    Input

    The input has several data sets separated by an empty line, each data set having the following format:

    On the first line - the number N
    On the second line - the number M
    On the following M lines - the digits X1,X2..XM.

    Output

    For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.

    An example of input and output:

    Sample Input

    Sample Output

    Source

    Southeastern Europe 2000

    这个题目可能没那么爽啊,就是给你m个数字,然后用这些数字就构造n的倍数

    首先肯定贪心取啊,然后就是大数取余,所以这个时候同余的,然后bfs就完了

     

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    const int N=5005;
    struct T
    {
        int res,pre,digit;
    } q[N],t;
    int n,m,a[20];
    bool vis[N];
    void print (int x)
    {
        if(q[x].pre==-1)return;
        print(q[x].pre),printf("%d",q[x].digit);
    }
    int bfs()
    {
        q[0]= {0,-1,0},memset(vis,0,sizeof vis);
        int tot=0,lst=1;
        while(tot<lst)
        {
            t=q[tot];
            for(int i=0,t1; i<m; i++)
            {
                t1=(t.res*10+a[i])%n;
                if(!a[i]&&t.pre==-1)continue;
                if(!vis[t1])vis[t1]=1,q[lst++]= {t1,tot,a[i]};
                if(!t1)
                {
                    print(lst-1),printf("
    ");
                    return 1;
                }
            }
            tot++;
        }
        return 0;
    }
    
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=0; i<m; i++)scanf("%d",&a[i]);
            if(!n)printf("0
    ");
            else
            {
                std::sort(a,a+m);
                if(!bfs())printf("0
    ");
            }
        }
    }

     

     

  • 相关阅读:
    利用Telnet来模拟Http请求 有GET和POST两种
    WebConfig特殊字符的转义!
    userprofile同步用户失败的原因和解决方案
    linux mysql表名大小写
    web.py 中文模版报错
    docker 开启远程
    web.py 笔记
    python 安装influxdb-python
    安装pip
    influxdb 命令
  • 原文地址:https://www.cnblogs.com/BobHuang/p/9449904.html
Copyright © 2011-2022 走看看