luogu P1602 Sramoc问题

嗯。。。这篇题解写的原因是一位大佬网友问我的题

本蒟蒻为了纪念下这一刻,就写了


我只会写一写基本思路,经不起推敲

还是大家凑活看吧

重点来了

在bfs时,队列里的每个元素由一个高精度的数和那个数模m的值

拓展节点时如果拓展得到的余数为零,直接返回输出即可

要是这个余数不为零且之前没有出现过,就加入队列,之前出现过,就舍弃

这就是我的思路,再附上Code

 

#include<bits/stdc++.h>
#define LL long long int
using namespace std;
const int maxn=1005,INF=2000000000,P=1000000007;
int K,M;
LL u,k;
bool vis[maxn];
queue<LL> q;
queue<vector<int> > q2;
vector<int> s;
void bfs() {
    for(int i=1; i<K; i++) {
        q.push(i%M);
        vis[i%M]=true;s.push_back(i);
        q2.push(s);s.pop_back();
    }
    while(!q.empty()) {
        u=q.front();q.pop();
        s=q2.front();q2.pop();
        for(int i=0; i<K; i++) {
            k=(u*10+i)%M;s.push_back(i);
            if(!k) {
                for(unsigned int j=0; j<s.size(); j++) printf("%d",s[j]);
                cout<<endl;return;
            } else if(!vis[k]) {
                vis[k]=true;
                q.push(k);q2.push(s);
            }
            s.pop_back();
        }
    }
}
int main() {
    cin>>K>>M;
    bfs();
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章