hihoCoder挑战赛11 益智游戏

题目链接:http://hihocoder.com/problemset/problem/1165


题面:

益智游戏

时间限制: 20000ms
单点时限: 1000ms
内存限制: 256MB

描述

幽香今天心情不错,正在和花田里的虫子玩一个益智游戏。
这个游戏是这样的,对于一个数组A,幽香从A中选择一个数a,虫子从A中选择一个数b。a和b可以相同。她们的分数是a*b的因子的个数。
幽香和虫子当然想要获得尽可能的高的分数,你能告诉她们应该选择哪两个数吗。
由于幽香是个非常随意的人,数组A中的元素都是她随机选择的。

输入

一行一个数n,表示A中整数的数量。
接下来一行n个数,分别表示a1,a2,...,an,为A中的元素。

n <= 100000, 1 <= ai <= 100000
保证所有的ai都是随机生成的。

输出

一行表示最大的分数。

样例输入
2
3 4
样例输出
6

解题:白天刚好研究了下质数分解的问题,刚好拿代码来套一下正好。第一次拿T-shirt,刚刚好卡在最后,昨天还因为错过T-shirt伤心,今天第一次拿到了T-shirt高兴到爆啊!!接下来要努力刷有质量的题,希望能有所进步。

还是不废话了,说下这道题吧。一个数的因子数等于它完全分解为质数后,每一项质数个数加一连乘的结果。比如75=3*5*5,那么它的因子数为(1+1)*(2+1)=6。其实这个结论特别好证明,每一个因子数可以取0到它的个数个。比如,每个都取0个,那么结果即为1。如果每个都取,那么结果为那个数本身。预处理下1-100000的因子数,然后将读进来的数按因子数多少排个序,然后取其前n(当n<200)或者前200个计算,取最大值即可。


代码:

#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;
map <int,int> store;
bool is_prime(unsigned int x)
{
	if(x==1)return false;
	else if(x==2)return true;
	int y=sqrt(1.0*x);
	for(int i=2;i<=y;i++)
	{
		if(x%i==0)
		return false;
	}
	return true;
}
void cal(unsigned int x)
{
	if(is_prime(x))
	store[x]++;
	else
	{
		int y=sqrt(1.0*x);
		for(int i=2;i<=y;i++)
		{
			if(x%i==0)
			{
				cal(i);
				cal(x/i);
				break;
			}
		}
	}
}
int ans[100005];
struct info
{
	int key,val;
}tempstore[100005];
bool cmp(info a,info b)
{
	return a.val>b.val;
}
int main()
{
	  ans[1]=1;
	  ans[2]=2;
	  ans[3]=2;
	  int tmp,res,maxx;
	  for(int i=4;i<=100000;i++)
	  {
	  	res=1;
	    store.clear();
	    int up_limit=sqrt(1.0*i);
	    tmp=i;
	    for(int j=2;j<=up_limit;j++)
	    {
  	       if(tmp%j==0)
		   {
		     cal(j);
			 cal(tmp/j);
			 break;
		    }
   	   }	
   	    if(store.size()==0)
		 {
		   ans[i]=2;
		   continue;
		 }
   	  map <int,int> ::iterator  iter;
   	  for(iter=store.begin();iter!=store.end();iter++)
   	  res*=((iter->second)+1);
   	  ans[i]=res;
	}
	int n;
	while(cin>>n)
	{
	  maxx=0;
	  for(int i=0;i<n;i++)
	  {
  	    cin>>tmp;
	    tempstore[i].key=tmp;
	    tempstore[i].val=ans[tmp];	
  	  }	
  	  sort(tempstore,tempstore+n,cmp);
  	  int z=min(200,n);
  	  for(int i=0;i<z;i++)
  	  {
  	  	  for(int m=0;m<z;m++)
  	  	  {
  	  	  	long long w=((long long int )tempstore[i].key)*tempstore[m].key;
  	  	  	store.clear();
  	  	    res=1;
	        int up_limit=sqrt(1.0*w);
	        //cout<<"*\n";
	       for(int j=2;j<=up_limit;j++)
	      {
  	         if(w%j==0)
		     {
		       cal(j);
			   cal((int)w/j);
			   break;
		      }
   	      }	
   	      //cout<<"@\n";
   	      if(store.size()==0)
		  {
		   res=2;
		  }
   	      map <int,int> ::iterator  iter;
   	      for(iter=store.begin();iter!=store.end();iter++)
   	      res*=((iter->second)+1);
   	      if(res>maxx)
   	      maxx=res;
   	      //cout<<"&\n";
  	    }
	 }
	 cout<<maxx<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值