题目大意: 中文题自己看→_→ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
解题思路: 500位不能用N的倍数去一个一个判断。要用bfs一个一个数地加。每加一个数要%N一次防爆。因为每个数都是余数,舍掉的N的倍数的前缀没有影响,故余数相同的情况是同一种情况,用vis记录并判重。如第二个测试数据,余数永远是1,不可能除尽2,若不用余数判重,必须算到501位才能得出结果。
ac代码:
#include<iostream> #include<queue> #include<cstring> #include<string> #include<algorithm> using namespace std; int T,N,C,M; int k; char c; int a[16]; bool vis[10000]; string ans; struct node { int num; string str; int len; node(int n=0,string s="",int l=0) { num = n; str = s; len = l; } }; char toChar(int x) { if(x<10) return x + '0'; else return x - 10 + 'A'; } void bfs() { queue<node> que; node p,pp; for(int i=0;i<M;i++) { if(a[i]==0)//没有0开头的数 continue; p.num = a[i]; p.str = toChar(a[i]); p.len = 1; que.push(p); } while(!que.empty()) { p = que.front(); que.pop(); if(p.num%N==0) { ans = p.str; return ; } if(p.len>500) continue; for(int i=0;i<M;i++) { pp = p; pp.str = pp.str+toChar(a[i]); pp.num = (pp.num*C + a[i]) % N; pp.len++; if(!vis[pp.num]) { vis[pp.num] = 1; que.push(pp); // cout<<"将"<<pp.str<<"压入队列\n"; } } } ans = ""; } int main() { ios::sync_with_stdio(false); cin>>T; while(T--) { k = 0;//判断M个数中有没有0 memset(vis,0,sizeof(vis)); cin>>N>>C; cin>>M; for(int i=0;i<M;i++) { cin>>c; if(c>='0'&&c<='9') a[i] = c - '0'; else a[i] = c - 55; if(!a[i]) k = 1; //有0 } if(!N&&k)//N==0且有0 { cout<<0<<endl; continue; } if(!N&&!k)//N==0但没0 { cout<<"give me the bomb please"<<endl; continue; } sort(a,a+M);//后面的队列先加入小的数,得到的第一个答案就是最小的密码 bfs(); if(ans=="") cout<<"give me the bomb please"<<endl; else cout<<ans<<endl; } return 0; }