Codeforces Round #696 (Div. 2) B. Different Divisors

B. Different Divisors

link

题意

\(T\) 组数据,每组数据给定一个数字 \(d\) ,求出一个最小的数字 \(x\) 满足 \(x\) 至少有 \(4\) 个因数,并且任意两个因数之差大于等于 \(d\)

数据范围

\(1 \le T \le 3000, 1 \le d \le 10000\)

SOLUTION

由于至少有 \(4\) 个因数,并且任意两个因数之差大于等于 \(d\)容易得出因数越多越劣。因此考虑枚举 \(x\) 除了 \(1,x\) 之外 的两个因数 \(a, b\),由于 \(x\) 只有 \(4\) 个因数,因此 \(a, b\) 都是质数,并且 \(a \ge 1 + d, b \ge a + d\),考虑预处理质因数,二分求解 \(a, b\) 即可。

CODE

点击查看代码
/* Generated by powerful Codeforces Tool
 * Author: SmartNanfeng
 * Time: 2022-09-08 22:35:01
**/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#ifdef LOCAL
#include <debugger>
#else
#define debug(...) 42
#endif

template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }

constexpr int N = 3E4 + 10;

int primes[N], cnt;
bool st[N];

void init() {
  int n = N - 1;
  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;
      }
    }
  }
}

void solve2() {
  int d; cin >> d;
  int idx = lower_bound(primes, primes + cnt, d + 1) - primes;
  int x = primes[idx];
  idx = lower_bound(primes, primes + cnt, x + d) - primes;
  int y = primes[idx];
  cout << x * y << "\n";
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  init();
  int T = 1; cin >> T;
  while (T --) solve2();
  return 0;
}

/*
 *
 *  ┏┓   ┏┓+ +
 * ┏┛┻━━━┛┻┓ + +
 * ┃       ┃
 * ┃   ━   ┃ ++ + + +
 *  ████━████+
 *  ◥██◤ ◥██◤ +
 * ┃   ┻   ┃
 * ┃       ┃ + +
 * ┗━┓   ┏━┛
 *   ┃   ┃ + + + +Code is far away from  
 *   ┃   ┃ + bug with the animal protecting
 *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
 *   ┃        ┣┓
 *    ┃        ┏┛
 *     ┗┓┓┏━┳┓┏┛ + + + +
 *    ┃┫┫ ┃┫┫
 *    ┗┻┛ ┗┻┛+ + + +
 */

posted @ 2022-09-13 18:15  ccz9729  阅读(23)  评论(0编辑  收藏  举报