利用STL的set可以使查询效率变高(2016.4浙江科技学院校赛)

   Water's coin

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 455   Accepted Submission(s) : 72

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Mr. Water find a interesting machine in street. There is a number n showed on the screen. When he put x coins in it, the machine will return y coins to him. And after trying several times , Mr. Water find the number of y is equal to the front n digits of x*x。Mr. Water want to know how much he can get at most, can you tell him?

Notes: Water is a doubi, every time he will put all his coins in machine.

Input

Input contains an integer T(1<=T<=200) in the first line, and then T lines follow. Each line consists of a pair of integers n(1<=n<=9)and x(0<=x<10^n), separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the max number of coins Mr. Water can get.
The first test
6->36
3->9
9->81
8->64
6->36

Sample Input

2
1 6
2 9

Sample Output

9
81


/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <vector>
#include <set>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define uLL unsigned long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 1000000007
#define MAX 105
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
/*---------------------Work-----------------------*/
set<LL>st;
set<LL>::iterator it; //set迭代器
void work()
{
	int T; cin>>T;
	while(T--)
	{
		st.clear();
		int n;
		LL x;
		scanf("%d%I64d",&n,&x);
		LL Max=x;
		while((it=st.find(x))==st.end()) //判断当前x是否是当前最大且已经出现过
		{
			st.insert(x);
			x=x*x;
			int cnt=0;
			LL temp=x;
			while(temp!=0)
			{
				temp=temp/10;
				cnt++;
			}
			if(cnt>n)
				x=x/pow(10,cnt-n);
			Max=max(Max,x);
		}
		printf("%I64d\n",Max);
	}
}
/*------------------Main Function------------------*/
int main()
{
	//freopen("test.txt","r",stdin);
	work();
	return 0;
}



你可能感兴趣的:(利用STL的set可以使查询效率变高(2016.4浙江科技学院校赛))