嗯。。。这篇题解写的缘由是一位大佬网友问个人题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; }