Asgard King(埃氏筛法)

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您需要使用 Python 编写爬虫程序,使用 urllib3 库进行网页请求,使用 re 库进行网页内容的正则匹配,使用 json 库对匹配到的数据进行处理,最后将处理后的数据存储到 top100.txt 文件中。 以下是示例代码: ```python import urllib3 import re import json # 创建 urllib3.PoolManager 对象 http = urllib3.PoolManager() # 定义目标网页 URL url = 'https://m.maoyan.com/asgard/board/4' # 发送 HTTP 请求,获取网页内容 response = http.request('GET', url, headers={'User-Agent': 'Mozilla/5.0'}) html_content = response.data.decode('utf-8') # 使用正则表达式匹配电影信息 pattern = r'<div class="movie-item-info">.*?' \ r'<p class="name">(?P<name>.*?)</p>.*?' \ r'<p class="star">(?P<actors>.*?)</p>.*?' \ r'<p class="releasetime">(?P<release_date>.*?)</p>.*?' \ r'<p class="score"><i class="integer">(?P<score1>.*?)</i>' \ r'<i class="fraction">(?P<score2>.*?)</i></p>.*?</div>.*?' \ r'<div class="movie-item-number score-num"><p class="realtime">.*?' \ r'<i class="board-index board-index-.*?">(?P<rank>\d+)</i>.*?' \ r'<img data-src="(?P<image>.*?)".*?>.*?</div>' matches = re.finditer(pattern, html_content, re.S) # 遍历匹配结果,构造电影字典,并将所有电影保存到列表中 movies = [] for match in matches: movie_dict = match.groupdict() movie_dict['actors'] = movie_dict['actors'].strip()[3:] movie_dict['score'] = float(movie_dict['score1'] + movie_dict['score2']) del movie_dict['score1'] del movie_dict['score2'] movies.append(movie_dict) # 将电影列表保存到 top100.txt 文件中 with open('top100.txt', 'w', encoding='utf-8') as f: f.write(json.dumps(movies, ensure_ascii=False, indent=4)) ``` 该程序会爬取猫眼电影 Top 100 网页,并使用正则表达式从网页内容中提取出电影排名、图片 URL、电影名称、主演、上映日期、得分等信息,最后将所有电影信息以 JSON 格式保存到 top100.txt 文件中。注意需要添加请求头,否则猫眼电影会拒绝请求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值