zoukankan      html  css  js  c++  java
  • [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) 
    对于每组数据输入一个区间[A,B]其中(1< =A< =B< =1000000) 
     
    Output
    输出满足条件的那个数。
     
    Sample Input
    2
    2 6
    20 200

    Sample Output
    6
    180

     
     
    解题思路:在判断一个数是否是素数时,我们就已经知道一个数x的因子是不会大于√x,这里算上本生(除去平方之外,每一个i*j增加两个因子),
         按照打素数表的思路,i,j,循环i*i<max,i*j<maxn为界
         dp[i*i]+=1(两个相同因子),dp[i*j]+=2,然后在给定区间最大值找max_dp,输出下标即可~~~
     
    代码如下:
     
     1 #include <stdio.h>
     2 using namespace std;
     3 int dp[1000010];
     4 void init(){
     5     for (int i = 1; i*i <= 1000000; i++){
     6         dp[i*i] += 1;
     7         for (int j = i + 1; i*j <= 1000000; j++){
     8             dp[i*j] += 2;
     9         }
    10     }
    11 }
    12 int main()
    13 {
    14     init();
    15     int t, l, r;
    16     scanf("%d", &t);
    17     while (t--){
    18         scanf("%d%d", &l, &r);
    19         int ans = l;
    20         for (int i = l; i <= r; i++){
    21             if (dp[i] > dp[ans])
    22                 ans = i;
    23         }
    24         printf("%d
    ", ans);
    25     }
    26     return 0;
    27 }
    View Code
     
  • 相关阅读:
    pytest
    pytest 跳过测试用例 skip
    pytest
    pytest -allure标记用例级别severity
    pytest-
    pytest-分布式执行(pytest-xdist)
    4、win10搭建easy-mock平台
    pandas日期缺失,空值填充方法
    Pandas拼接操作(concat,merge,join和append)的区别
    关于docker容器和镜像的区别
  • 原文地址:https://www.cnblogs.com/zyxStar/p/4582230.html
Copyright © 2011-2022 走看看