Codeforces 1474 B. Different Divisors (prime sieve/linear sieve)

Insert picture description here

The solution given by the official website
Insert picture description here

General meaning

a has at least 4 factors, and the difference between any pair of factors of a is greater than or equal to d. Find the smallest a

Analyze
the factor that we find the number a. First of all, 1 must be its factor. It must be ensured that the difference between any two factors of a must be greater than or equal to d. Suppose we find two adjacent factors m, n satisfy the difference greater than or equal to d, but If m or n can also be decomposed into smaller factors, then there may be differences in their sub-factors that cannot meet the conditions, so we need to require prime factors. We use prime number sieve to store prime numbers in primes[ ], and then find prime factors in the array to satisfy the condition.

C++ code

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std;
 
const int N = 1e7+5;
 
int primes[N], cnt;     // primes[]存储所有素数
bool st[N];         // st[x]存储x是否被筛掉
 
void get_primes(int n)
{
    
    
    for (int i = 2; i <= n; i ++ )
    {
    
    
        if (!st[i]) primes[cnt ++ ] = i;
        for (int j = 0; primes[j] <= n / i; j ++ )
        {
    
    
            st[primes[j] * i] = true;
            if (i % primes[j] == 0) break;
        }
    }
}
 
bool check(int n)  
{
    
      
    for (int i=0; primes[i]*primes[i]<=n; i++)  
        if (n%primes[i]==0)  
            return false;  
    return true;  
}  
long long ans[100005];
int main()
{
    
    
    
    get_primes(1e6);
 
    int t;
    cin >> t;
    while(t--)
    {
    
    
        int d; cin >> d;
        if(d==1)
        {
    
    
            cout << 6<<endl;
            continue;
        }
        long long minn = 1e9;
        for(int i = 1; i < cnt; i ++)
        {
    
    
            int flog = 0;
            if(primes[i] - 1 >= d)
            {
    
    
                for(int j = i; j < cnt;  j++)
                {
    
    
                    if(primes[j] - primes[i] >= d)
                    {
    
    
                    cout << primes[i]*primes[j] << endl;
                    flog = 1;
                    break;
                    }
                } 
            }
            if(flog == 1)break;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/diviner_s/article/details/112907462