【HDU 4394】Digital Square(bfs,数位搜索,思维,数学)
題干:
Given an integer N,you should come up with the minimum?nonnegative?integer M.M meets the follow condition: M?2%10?x=N (x=0,1,2,3....)
Input
The first line has an integer T( T< = 1000), the number of test cases.?
For each case, each line contains one integer N(0<= N <=10?9), indicating the given number.
Output
For each case output the answer if it exists, otherwise print “None”.
Sample Input
3 3 21 25Sample Output
None 11 5題目大意:
解題報告:
N的個位只由M的個位決定,N十位由M的個位和十位決定,N的百位由M的個位十位百位決定,以此類推。
所以可以直接bfs優先隊列式搜索,先確定個位,再確定十位,再確定百位,以此類推,因為N的范圍是1e9,所以M也可以直接在1e9范圍內搜索,因為當前搜索的那一位如果不滿足的話就不會入隊,所以不需要加其他的break條件就可以結束bfs。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; struct Node {ll val,step;Node(ll val=0,ll step=0):val(val),step(step){}bool operator < (const Node & b)const {return val>b.val;} }; ll bfs(ll n) {priority_queue<Node> pq;pq.push(Node(0,1));while(!pq.empty()) {Node cur = pq.top();pq.pop();if((cur.val*cur.val)%cur.step == n) return cur.val;Node now = cur;now.step *= 10;for(ll i = 0; i<10; ++i) {now.val = cur.val+i*cur.step;if((now.val*now.val)%now.step == n%now.step) {pq.push(now);}}}return -1; }int main() {int t;scanf("%d",&t);while(t--) {ll n;scanf("%lld",&n);ll ans = bfs(n);if(ans == -1) puts("None");else printf("%lld\n",ans);}return 0; }?
總結
以上是生活随笔為你收集整理的【HDU 4394】Digital Square(bfs,数位搜索,思维,数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SPBBCSvc.exe - SPBBC
- 下一篇: speedupmypc.exe - sp