zoukankan      html  css  js  c++  java
  • spoj 370. Ones and zeros(搜索+同余剪枝+链表存数(可能越界LL))

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<set>
    #include<map>
    #include<list>
    #include<queue>
    #include<vector>
    #define tree int o,int l,int r
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define lo o<<1
    #define ro o<<1|1
    #define ULL unsigned long long
    #define LL long long
    #define UI unsigned int
    #define inf 0x7fffffff
    #define eps 1e-7
    #define M 29901
    #define N 1000009
    using namespace std;
    int T,n,m,k,t,maxv;
    struct Node
    {
        int c,last;
    } node[M];
    bool res[N];
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("ex.in","r",stdin);
    #endif
        int ncase=0;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            memset(node,-1,sizeof(node));
            int ok=0;
    
            node[1].c=1;
            node[1].last=-1;
            queue<int>q;
            q.push(1);
            while(!q.empty())
            {
                int yu=q.front();
                q.pop();
                for(int i=0; i<2; i++)
                {
                    int tp=(yu*10+i)%n;
                    if(node[tp].c==-1)
                    {
                        node[tp].c=i;
                        node[tp].last=yu;
                        if(tp==0)//只找第一个,肯定是最小的!
                        {
                            ok=1;
                            break;
                        }
                        q.push(tp);
                    }
                }
                if(ok)break;
            }
            int sub=0,top=0;
            while(sub!=-1)
            {
                res[top++]=node[sub].c;
                sub=node[sub].last;
            }
            for(int i=top-1; i>=0; i--)
                printf("%d",res[i]);
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    2021.3.3
    2021.3.2
    2021.3.1
    2021.2.28(每周总结)
    2021.2.27
    2021.2.26
    2021.2.25
    2021.2.23
    Redis系统学习之五大基本数据类型(List(列表))
    Redis系统学习之五大基本数据类型(String(字符串))
  • 原文地址:https://www.cnblogs.com/sbaof/p/3243481.html
Copyright © 2011-2022 走看看