UVA-294 Divisors(线性筛+唯一分解定理)

本文介绍了一种算法,用于在给定范围内找到拥有最多正除数的整数及其除数数量。通过分解质因数的方法,该算法能够高效地处理大规模数值范围,适用于数学和计算机科学领域的研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 interesting numbers, you are to write a program that scans a range of numbers and determines the number that has the largest number of divisors in the range. Unfortunately, the size of the numbers, and the size of the range is such that a too simple-minded approach may take too much time to run. So make sure that your algorithm is clever enough to cope with the largest possible range in just a few seconds.

Input

     The first line of input specifies the number N of ranges, and each of the N following lines contains a range, consisting of a lower bound L and an upper bound U, where L and U are included in the range. L and U are chosen such that 1 ≤ L ≤ U ≤ 1000000000 and 0 ≤ U − L ≤ 10000.

Output

       For each range, find the number P which has the largest number of divisors (if several numbers tie for first place, select the lowest), and the number of positive divisors D of P (where P is included as a divisor). Print the text ‘Between L and H, P has a maximum of D divisors.’, where L, H, P, and D are the numbers as defined above.

Sample Input

3

1 10

1000 1000

999999900 1000000000

Sample Outpu

t Between 1 and 10, 6 has a maximum of 4 divisors.

Between 1000 and 1000, 1000 has a maximum of 16 divisors.

Between 999999900 and 1000000000, 999999924 has a maximum of 192 divisors.

题意是两个数n,m,要你求[n,m]之间因数最多的数和这个数的因数个数

解题思路:

        对于每个大于一的数,若本身不是素数,则一定可以被分解成若干个素数的乘积形式。

        比如12的唯一表达式为12 = 2 ^ 2 * 3 ^ 1, 24的唯一表达式为24 = 2 ^ 3 * 3 ^ 1

        首先,我们先通过线性筛得到1000000000以内的全部素数。然后对于当前的n进行循环判断是否为下标为i的素数的倍数,是则n/=primeNum[i],计数器tmp++,否则循环结束,res *= tmp,然后开始判断下一个素数

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int maxN = 36000;

int primeNum[maxN], cou = 0;
bool isUsed[maxN];

int Fun(int n) {
    int res = 1, tmp;
    for(int i = 0;i < cou && n != 1;i++) {
        tmp = 1;
        if(n % primeNum[i] == 0) {
            while(n % primeNum[i] == 0) {
                tmp++;
                n /= primeNum[i];
            }
        }
        res *= tmp;
    }
    return res;
}

int main() {
    memset(isUsed,0,sizeof(isUsed));
    for(int i = 2;i <= maxN;i++) {
        if(!isUsed[i]) {
            primeNum[cou++] = i;
        }
        for(int j = 0;j < cou && i * primeNum[j] <= maxN;j++) {
            isUsed[i * primeNum[j]] = true;
            if(i % primeNum[j] == 0){
                break;
            }
        }
    }
    int t, n, m, res, num;
    scanf("%d",&t);
    while(t--) {
        res = 0;
        num = 0;
        scanf("%d%d",&n,&m);
        for(int i = n;i <= m;i++) {
            if(Fun(i) > res) {
                res = Fun(i);
                num = i;
            }
        }
        printf("Between %d and %d, %d has a maximum of %d divisors.\n", n, m, num, res);
    }
    return 0;
}

 

文件1:第一行 {"index": 0, "prompt": {"data": ["Suppose $a$ and $b$ are different prime numbers greater than 2. How many whole-number divisors are there for the integer $a(2a+b)-2a^{2}+ab$?\nPlease reason step by step, and put your final answer within \\boxed{}."], "system_prompt": null, "raw_input": {"problem": "Suppose $a$ and $b$ are different prime numbers greater than 2. How many whole-number divisors are there for the integer $a(2a+b)-2a^{2}+ab$?", "solution": "Distributing and combining like terms, we have $a(2a+b)-2a^2+ab=2a^2+ab-2a^2+ab=2ab$. Now $a$ and $b$ are different prime numbers greater than 2, so $2ab=2^1\\cdot a^1\\cdot b^1$ has $(1+1)(1+1)(1+1)=\\boxed{8}$ divisors.", "answer": "8", "subject": "Number Theory", "level": 4, "unique_id": "test/number_theory/1287.json"}}, "tags": ["en"], "categories": ["DeepSeekDistill", "Math"], "task_type": "math", "weight": 0.3333333333333333, "dataset_name": "math_500", "subset_name": "default"} 文件2:第281行 {"problem": "Suppose $a$ and $b$ are different prime numbers greater than 2. How many whole-number divisors are there for the integer $a(2a+b)-2a^{2}+ab$?", "solution": "Distributing and combining like terms, we have $a(2a+b)-2a^2+ab=2a^2+ab-2a^2+ab=2ab$. Now $a$ and $b$ are different prime numbers greater than 2, so $2ab=2^1\\cdot a^1\\cdot b^1$ has $(1+1)(1+1)(1+1)=\\boxed{8}$ divisors.", "answer": "8", "subject": "Number Theory", "level": 4, "unique_id": "test/number_theory/1287.json"} 写一个脚本依据problem字段的值查找两个文件中的所有问题是否一致,对比两个文件中的problem是否相同,两个文件中的该部分内容并不在同一行,如果不一致输出不一样的内容并将打印内容输出为一个文件
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值