当前位置: 首页 > 工具软件 > AsGard > 使用案例 >

Asgard King(埃氏筛法)

华永逸
2023-12-01

Description

Thor had great power, but his arrogant and reckless behavior set off an ancient war, and he was demoted into the world as punishment, forced to live with human beings, and finally Thor learned to become a superhero, and Thor's home in Asgard. 
Asgard is said to be separated from Midgard in the human world. However, some early researchers pointed out that Asgard was probably a famous castle and was mistaken for the realm of God. 
    Asgard is the home of the avenger Thor, the God of thunder, where technology is advanced and even the king is a smart person. Now the king is making plans for the next king. At the suggestion of ACMer, the king asked a question. There are N places in Asgard, and each place has its corresponding number Ai. Now the King wants the candidates to sort these numbers, and the ranking is from small to large according to the number of factors in each number. When the number of factors is the same, the number is sorted according to the size of the number, and the final K element can be output. 

Input

The first line input two positive integers N, K(1 <= k <= N <= 1e6) 
The second line input N positive integers a[i] (a[i] <= 1e6) 

Output

Output number K in the array.

 

 

Sample Input

3  1

4 6 9

Sample Output

4

 

题意:给你N个数,然后按照每个数的因子个数进行排序(从小到大),如果因子个数相同,则按照实际值的大小排序;

由于:需要找每个数的因子个数,所以一开始我想到了用欧拉筛加唯一分解定理进行预处理,没有算好时间,超时了。最后想了一下,确实会超时。最后让我很无奈。还是太菜了。经过大佬的讲解,我去看一下埃氏筛。

我觉得就是欧拉筛的简化般,标记的倍数,没标记一次,因子个数就加1;

举例:

1 2 3 4 5 6 7 8 9

第一次 标记以1为因子的数:vis[1*1]++  vis[1*2]++  vis[1*3]++ ........

第二次标记以2为因子的数:vis[2*1]++  vis[2*2]++  vis[2*3]++  ........

第三次标记以3为因子的数: vis[3*1]++  vis[3*2]++  vis[3*3]++ .......

............

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1e6+5;
int ans[maxn];
int vis[maxn];
void init()
{
    for(int i=1;i<=maxn;i++)
        for(int j=1;i*j<maxn;j++)
                ans[i*j]++;
}
struct node
{
    int x,y;
}mm[maxn];
int  cmb(node fx,node fy)
{
    if(fx.y==fy.y)
        {
            return fx.x<fy.x;
        }
        else
        {
           return fx.y<fy.y;
        }
}
int main()
{
   init();
    int N,K,xx;
    cin>>N>>K;
    for(int i=1;i<=N;i++)
    {
        cin>>xx;
       mm[i].x=xx;
       mm[i].y=ans[xx];///因子个数
    }
    sort(mm+1,mm+N+1,cmb);
    cout<<mm[K].x;
    return 0;
}

 

 类似资料: