UVA - 294 Divisors (约数)(数论)

news/2024/7/8 3:39:55

题意:输入两个整数L,U(1<=L<=U<=109,U-L<=10000),统计区间[L,U]的整数中哪一个的正约数最多。如果有多个,输出最小值。

分析:

1、求一个数的约数,相当于分解质因子。

2、例如60 = 2 * 2 * 3 * 5。对于2来说,可选0个2,1个2,2个2,有3种情况,同理对于3,有2种情况,对于5,有2种情况,所以3 * 2 * 2则为60的约数个数。

3、L到U扫一遍,取最大值即可。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
    if(fabs(a - b) < eps)  return 0;
    return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 35000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int vis[MAXN];
vector<int> prime;
void init(){
    for(int i = 2; i < MAXN; ++i){
        if(!vis[i]){
            prime.push_back(i);
            for(int j = 2 * i; j < MAXN; j += i){
                vis[j] = 1;
            }
        }
    }
}
int cal(int n){
    int ans = 1;
    int len = prime.size();
    for(int i = 0; i < len; ++i){
        if(prime[i] > n) break;
        if(n % prime[i]) continue;
        int cnt = 1;
        while(n % prime[i] == 0){
            ++cnt;
            n /= prime[i];
        }
        ans *= cnt;
    }
    return ans;
}
int main(){
    init();
    int T;
    scanf("%d", &T);
    while(T--){
        int l, r;
        scanf("%d%d", &l, &r);
        int ans = 0;
        int id;
        for(int i = l; i <= r; ++i){
            int tmp = cal(i);
            if(tmp > ans){
                ans = tmp;
                id = i;
            }
        }
        printf("Between %d and %d, %d has a maximum of %d divisors.\n", l, r, id, ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/6391047.html


http://www.niftyadmin.cn/n/3124512.html

相关文章

9个 SSH常用命令选项

9个 SSH常用命令选项 SSH 是什么 SSH&#xff08;全称 Secure Shell)是一种加密的网络协议。使用该协议的数据将被加密&#xff0c;如果在传输中间数据泄漏&#xff0c;也可以确保没有人能读取出有用信息。要使用 SSH&#xff0c;目标机器应该安装 SSH 服务端应用程序&#xff…

关于js中namespace命名空间模式

命名空间有助于减少程序中所需要的全局变量的数量&#xff0c;并且同时有助于避免命名冲突或过长的名字前缀。 关于命名空间的例子&#xff1a; 1 /**2 * 创建全局对象MYAPP3 * module MYAPP4 * title MYAPP Global5 */6 var MYAPP MYAPP || {};7 8 /**9 * 返回指定的命名空间…

react全家桶项目,cuckoo

https://gitee.com/guozia007/cuckoo

Ios转场动画,type动画类型,跳转控制器动画

2019独角兽企业重金招聘Python工程师标准>>> 转场动画是针对view图层的动画 #import "DYViewController.h"interface DYViewController () property (weak, nonatomic) IBOutlet UIImageView *imageView;property (nonatomic, assign) int index;endimple…

spark知识点_RDD

来自官网的Spark Programming Guide&#xff0c;包括个人理解的东西。 这里有一个疑惑点&#xff0c;pyspark是否支持Python内置函数&#xff08;list、tuple、dictionary相关操作&#xff09;&#xff1f;思考加搜索查询之后是这么考虑的&#xff1a;要想在多台机器上分布式处…

工作笔记——js前端规范

去年年末做了一个项目&#xff0c;因为第一次做前端管理职位&#xff0c;第一次做整个项目的前端架构很多东西都不熟悉&#xff0c;作为一次大胆的尝试。 js方面的只有一个坑&#xff0c;那就是前端与后端的网络层封装&#xff0c;这一块是在后端的协助下开发的。网络层封装的过…

ArcGIS Server开发教程系列(8)ArcGIS API for Javascript-控件(小部件)(续)纯代码...

完整代码如下&#xff1a; <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8"/><title>地图-Test </title><link href"arcgis_js_v39_api/arcgis_js_api/library/3.9/3.9compact/js/do…

后台多任务多线程断点下载

忘记图&#xff1a; 多线程断点下载事实上不是非常难。主要就是三个方面&#xff1a; 1、依据文件的大小和下载线程的数量&#xff0c;确定每一个下载线程要下载的切割文件的大小&#xff1b; 2、记录每一个下载线程已经下载完毕的进度。 3、将每一个线程下载的切割的文件合并到…