luogu P1602 Sramoc問題

嗯。。。這篇題解寫的緣由是一位大佬網友問個人題c++

本蒟蒻爲了記念下這一刻,就寫了spa


我只會寫一寫基本思路,經不起推敲code

仍是你們湊活看吧blog

重點來了隊列

在bfs時,隊列裏的每一個元素由一個高精度的數和那個數模m的值 ci

拓展節點時若是拓展獲得的餘數爲零,直接返回輸出便可 it

要是這個餘數不爲零且以前沒有出現過,就加入隊列,以前出現過,就捨棄class

這就是個人思路,再附上Codequeue

 

#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;
}
相關文章
相關標籤/搜索