hihocoder1165 益智游戏 (最多因子)

题目链接:

http://hihocoder.com/problemset/problem/1165

题意:

从n个中选出两个数来使他们乘积的因子的个数最多。

分析:

对于一个数我们将其进行素因子分解

x= (p1^a1)*(p2^a2)*....*(pn^an);

那么x的因子数为(a1+1)*(a2+1)*...*(an+1);

然后我们可以预处理1到100000的因子的个数,然后

对输入的数据按照因子的个数排下序,然后枚举前200

个数的组合,求一下最大值就好了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1e5+10;

int prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, };

int a[maxn][66],num[maxn];

void init(){
    for(int i=1;i<maxn-8;i++){
        int tmp = i;
        memset(a[i],0,sizeof(a[i]));
        for(int j=0;j<66&&prime[j]<=tmp;j++){
            if(tmp%prime[j]==0){
                while(tmp%prime[j]==0){
                    a[i][j]++;
                    tmp/=prime[j];
                }
            }
        }
    }
    for(int i=1;i<maxn-8;i++){
        num[i]=1;
        for(int j=0;j<66;j++){
            num[i]*=(a[i][j]+1);
        }
    }
}
struct ans{
    int a,b;
    bool operator <(const struct ans &tmp)const{
        return b>tmp.b;
    }
}p[maxn];

int main()
{
    init();
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            scanf("%d",&p[i].a);
            p[i].b=num[p[i].a];
        }
        sort(p,p+n);
        int rea = 0;
        if(n>200){
            for(int i=0;i<200;i++){
                for(int j=i;j<200;j++){
                    int tmp = 1;
                    for(int t=0;t<66;t++){
                        tmp*=(a[p[i].a][t]+a[p[j].a][t]+1);
                    }
                    rea=max(rea,tmp);
                }
            }
        }
        else{
            for(int i=0;i<n;i++){
                for(int j=i+1;j<n;j++){
                    int tmp = 1;
                    for(int t=0;t<66;t++){
                        tmp*=(a[p[i].a][t]+a[p[j].a][t]+1);
                    }
                    rea=max(rea,tmp);
                }
            }
        }
        printf("%d\n",rea);
    }
    return 0;
}



时间: 2024-08-02 17:34:06

hihocoder1165 益智游戏 (最多因子)的相关文章

Who Gets the Most Candies?(线段树 + 反素数 )

Who Gets the Most Candies? Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of th

[Swust OJ 217]--Factor(数论,类素数表)

题目链接:http://acm.swust.edu.cn/problem/0217/ Time limit(ms): 2000 Memory limit(kb): 65535 Description 给定一个数,如N=10,我们知道它有如下4个因子:1.2.5.10.现在的问题来了:给你一个区间[A,B],通过编程求出该区间内具有最多因子的那个数,如果有多个具有最多因子的数,输出最小的那个数. Input 首先输入一个数cas,代表下面共有cas组测试数据.(cas< =20) 对于每组数据输入

ZOJ3662Math Magic(分组背包+完全背包)

I - Math Magic Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common multiple) of two positive numbers c

推荐系统——隐因子的矩阵分解法

在新手接触推荐系统这个领域时,遇到第一个理解起来比较困难的就是协同过滤法.那么如果这时候百度的话,得到最多的是奇异值分解法,即(SVD).SVD的作用大致是将一个矩阵分解为三个矩阵相乘的形式.如果运用在推荐系统中,首先我们将我们的训练集表示成矩阵的形式,这里我们以movielen数据集为例.这个数据集包含了用户对电影的评分.那么矩阵形式大致为:   movie1 movie2 movie3 moive4 user1 1       user2 2     3 user3   5 4   user

UVA 294 - Divisors 因子个数

Mathematicians love all sorts of odd properties of numbers. For instance, they consider 945 to be an interesting number, since it is the first odd number for which the sum of its divisors is larger than the number itself. To help them search for inte

HDOJ(HDU) 2521 反素数(因子个数~)

Problem Description 反素数就是满足对于任意i(0< i < x),都有g(i) < g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[a,b],请你求出该区间的x使g(x)最大. Input 第一行输入n,接下来n行测试数据 输入包括a,b, 1<=a<=b<=5000,表示闭区间[a,b]. Output 输出为一个整数,为该区间因子最多的数.如果满足条件有多个,则输出其中最小的数. Sample Input 3 2 3

SQL Server索引的维护 - 索引碎片、填充因子 &lt;第三篇&gt;

实际上,索引的维护主要包括以下两个方面: 页拆分 碎片 这两个问题都和页密度有关,虽然两者的表现形式在本质上有所区别,但是故障排除工具是一样的,因为处理是相同的. 对于非常小的表(比64KB小得多),一个区中的页面可能属于多余一个的索引或表---这被称为混合区.如果数据库中有太多的小表,混合区帮助SQL Server节约磁盘空间. 随着表(或索引)增长并且请求超过8个页面,SQL Server创建专用于该表(或索引)的区并且从该区中分配页面.这样一个区被称为统一区,它可以为多达8个相同表或索引的

hihoCoder挑战赛11 益智游戏

题目链接:http://hihocoder.com/problemset/problem/1165 题面: 益智游戏 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 幽香今天心情不错,正在和花田里的虫子玩一个益智游戏. 这个游戏是这样的,对于一个数组A,幽香从A中选择一个数a,虫子从A中选择一个数b.a和b可以相同.她们的分数是a*b的因子的个数. 幽香和虫子当然想要获得尽可能的高的分数,你能告诉她们应该选择哪两个数吗. 由于幽香是个非常随意的人,数组A中的元素都是

Codeforces 757B:Bash&#39;s Big Day(分解因子+Hash)

http://codeforces.com/problemset/problem/757/B 题意:给出n个数,求一个最大的集合并且这个集合中的元素gcd的结果不等于1. 思路:一开始把素数表打出来,发现有9k+个数,不能暴力枚举.发现O(sqrt(n)*n)似乎可行,就枚举因子,然后出现过的因子就在Hash[]加1,最后枚举素数表里的元素,找出现次数最多的,因为那些数都可以映射在素数表里面.注意最少的ans是1. 1 #include <cstdio> 2 #include <algo