I Count Two Three题解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Little_Small_Joze/article/details/81189635

I will show you the most popular board game in the Shanghai Ingress Resistance Team.
It all started several months ago. 
We found out the home address of the enlightened agent Icount2three and decided to draw him out. 
Millions of missiles were detonated, but some of them failed. 

After the event, we analysed the laws of failed attacks. 
It's interesting that the ii-th attacks failed if and only if ii can be rewritten as the form of 2a3b5c7d2a3b5c7d which a,b,c,da,b,c,d are non-negative integers. 

At recent dinner parties, we call the integers with the form 2a3b5c7d2a3b5c7d "I Count Two Three Numbers". 
A related board game with a given positive integer nn from one agent, asks all participants the smallest "I Count Two Three Number" no smaller than nn.

Input

The first line of input contains an integer t (1≤t≤500000)t (1≤t≤500000), the number of test cases. tt test cases follow. Each test case provides one integer n (1≤n≤109)n (1≤n≤109).

Output

For each test case, output one line with only one integer corresponding to the shortest "I Count Two Three Number" no smaller than nn.

Sample Input

10
1
11
13
123
1234
12345
123456
1234567
12345678
123456789

Sample Output

1
12
14
125
1250
12348
123480
1234800
12348000
123480000

思路:

首先想到的是找规律,但是找了一下发现没什么头绪,然后就想到打个表继续找规律。

那么,就要思考一下怎么打表。第一种方法是最容易想到的,就是写四个循环,加一个判断条件ans<=1e9,打一维表,然后用sort排序,就得到了有序的1e9以内的表;第二种方法是直接按顺序打表,不用sort(),这样的话,就写一个循环for(int i=1;i<1e9;++i) 然后判断一下每一个i能不能被2,3,5,7整除到底,如果可以就放到表里,同样也得到了有序的1e9以内的表。

表打完了, 继续找规律,但是发现没有很明显的规律。

然后没有办法了,就尝试一下打表能不能过。然后,首先想到的是写辅助程序打表,然后把表复制粘贴到数组里,lower_bound(),输出。发现打表很长,整个程序500+行,因为程序太长提交不上去(不知道现场赛有没有这个情况),然后就打算在程序里打表。一开始感觉sort一个大数组用时会很长,所以用了第二种打表方法,发现不行,1~1e9的数枚举,满足条件被放进数组的只有5000个左右,加上跑了一下程序发现没个5、7秒出不来结果,直接判断是枚举没用的i占用时间太长,超时,所以还是改为采用第一种方法。调试了一下,最后AC了。

C++ AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
const int N =1e6+10;
using namespace std;
long long list[N];
int main(){
	int cnt=0;
	long long s2=1,s3=1,s5=1,s7=1;
	for(int a =0;a <32;a++)
	{
		if(a!=0) s2*=2;
		else s2=1;
		for(int b=0;b<20;b++)
		{
			if(b!=0) s3*=3;
			else s3=1;
			if(s2*s3>1e10) continue;
			for(int c=0;c<14;c++)
			{
				if(c!=0) s5*=5;
				else s5=1;
				if(s2*s3*s5>1e10) continue;
				for(int d =0;d <12;d ++)
				{
					if(d !=0) s7*=7;
					else s7=1;
					if(s2*s3*s5*s7>1e10) continue;
					list[cnt++]=s2*s3*s5*s7;
				}
			}
		}
	}

	sort(list,list+cnt);	
	int t,p;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&p);
		printf("%d\n",*lower_bound(list,list+cnt,p));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Little_Small_Joze/article/details/81189635