hdu 4394 Digital Square

Digital Square
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja
va/Others)
Total Submission(s): 2376 Accepted Submission(s): 996

Problem Description

Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition: M2%10x=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 <=109), indicating the given number.

Output

For each case output the answer if it exists, otherwise print “None”.

Sample Input

3
3
21
25

Sample Output

None
11
5

思路:
====== 从低位 向高位宽搜索
先试探个位, 如果个位合理, 则试探十位,百位,千位。。。。。。;如果合理就入队列;
一直到搜索到那个数就输出。

AC代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<fstream>
#include<ctime>
#include<vector>
#include<sstream> 
#include<cstdlib>
using namespace std;
int n;
struct node{
	long long int m;
	int len;
	node(int x, int y):m(x), len(y){}
	bool operator <(const node &x) const {
		return m > x.m; //每次选取最小的值来试探
	} 
};
priority_queue<node> q;
void dfs(){
	while(!q.empty()) q.pop();
	q.push(node(0,0));
	while(!q.empty()){
		node a = q.top();
		q.pop();
		int len = 1;
		for(int i = 0; i < a.len; i++){//代表已经选取了几位,然后后面对更高一位进行操作试探
			len *= 10;
		}
		if((a.m * a.m) % len == n){
			cout << a.m << endl;
			return;
		}
		
		for(int i = 0;i < 10; i++){ // 选值 
			node temp = node(0,0);
			temp.m = a.m + i * len;
			 temp.len = a.len + 1;
			 if((temp.m * temp.m) % (len * 10) == n % (len * 10)){//代表已经选取到合适的值
			 	q.push(temp);//选取到就入队	
			 }
		}
	}
	printf("None\n");
}
int main(){
 		int t;
 		cin >> t;
		while(t--){
			scanf("%d", &n);
			if(n == 2 || n == 3 || n == 7 || n == 8){
				printf("None\n");
			}else{
				dfs();		
			}

		}
	
	return 0;
}


发布了28 篇原创文章 · 获赞 22 · 访问量 1046

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/103207646