codeforces 1473b Different Divisors

题目如下
Positive integer 𝑥 is called divisor of positive integer 𝑦, if 𝑦 is divisible by 𝑥 without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.

We gave you an integer 𝑑 and asked you to find the smallest positive integer 𝑎, such that

𝑎 has at least 4 divisors;
difference between any two divisors of 𝑎 is at least 𝑑.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤3000) — the number of test cases.

The first line of each test case contains a single integer 𝑑 (1≤𝑑≤10000).

Output
For each test case print one integer 𝑎 — the answer for this test case.

题目大意
是要求我们找到满足1.至少有四个除数,2.除数之间相差最小为d 的最小正整数

思路
首先要满足至少4个除数,并且是满足条件最小的正整数,那么考虑到要得到满足条件最小,那么尝试将两个相差为d的质数相乘,每个质数分别有两个除数,那么也保证了得到的数有四个除数
首先确保两个除数都是质数

点击查看代码
bool is_prime(int x){
    if (x < 2)
        return false;
    int limit = sqrt(x);
    for(int i = 2; i <= limit; i++){
        if (x % i == 0)
            return false;
    }
    return true;
}
其次是找到符合条件的最小质数(除数) 首先是相差为d,通过 int f = 1 + d;和int s = f + d;来保证 其次是两个两个相邻的质数,通过下面的代码来实现
点击查看代码
while(!is_prime(f)){
            f++;
        }
        int s = f + d;
        while(!is_prime(s)){
            s++;
        }
完整代码如下
点击查看代码
#include <stdio.h>
#include <cmath>

bool is_prime(int x){
    if (x < 2)
        return false;
    int limit = sqrt(x);
    for(int i = 2; i <= limit; i++){
        if (x % i == 0)
            return false;
    }
    return true;
}
//满足条件的最小正整数
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int d;
        scanf("%d",&d);
        int f = 1 + d;
        while(!is_prime(f)){
            f++;
        }
        int s = f + d;
        while(!is_prime(s)){
            s++;
        }
        printf("%d\n", s * f);
    }
    return 0;
}
posted @ 2025-06-30 20:41  sirro1uta  阅读(15)  评论(0)    收藏  举报