标签:other java input 题目 否则 大于 algo 进制 pat
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4523 Accepted Submission(s): 1468
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <queue> #include <cmath> #include <vector> #include <map> using namespace std; int vis[5100]; int t; int n,c,num; char ch; vector<char>V; //用来记录可以使用的数字 struct node { string path; //bfs有时用string 记录路径很方便 int step; int mod; }; int get_mod(string x) //大数求余 { int ans=0; int tmp=x.size(); int h; for(int i=0;i<=x.size()-1;i++) { if(x[i]>=‘A‘) h=x[i]-‘A‘+10; else h=x[i]-‘0‘; ans=ans*c+h; ans=ans%n; } return ans; } string bfs() { memset(vis,0,sizeof(vis)); queue<node>Q; node a,next; a.step=0; a.path=""; a.mod=-1; Q.push(a); while(!Q.empty()) { a=Q.front(); Q.pop(); if(a.mod==0)return a.path; if(a.step>=500)break; next.step=a.step+1; for(int i=0;i<V.size();i++) { if(a.step==0&&V[i]==‘0‘&&n!=0)continue; next.path=a.path+V[i]; next.mod=get_mod(next.path); if(!vis[next.mod]) { vis[next.mod]=1; Q.push(next); } } } return ""; } int main() { scanf("%d",&t); string ans; while(t--) { V.clear(); scanf("%d%d",&n,&c); scanf("%d",&num); while(num--) { scanf(" %c",&ch); V.push_back(ch); } sort(V.begin(),V.end()); if(n==0) { if(V[0]==‘0‘)puts("0"); else puts("give me the bomb please"); continue; } ans=bfs(); if(ans=="")puts("give me the bomb please"); else cout<<ans<<endl; } return 0; }
标签:other java input 题目 否则 大于 algo 进制 pat
原文地址:http://www.cnblogs.com/a249189046/p/7620270.html