[CF1512G] Short Task - 筛法

Description

设 \(d(n)\) 表示 \(n\) 的所有因数的和,多组询问,给定 \(c \le 10^7\) 求最小的 \(n\) 满足 \(d(n)=c\)

Solution

预处理 \(d(n)\) 然后丢尽桶,询问时直接查即可

预处理可以暴力 \(O(n \log n)\),也可以利用线性筛预处理积性函数

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

#define int long long

const int N = 10000005;
int a[N], b[N];

signed main()
{
    ios::sync_with_stdio(false);

    for (int i = 1; i < N; i++)
        for (int j = i; j < N; j += i)
            a[j] += i;
    memset(b, 0x3f, sizeof b);
    for (int i = 1; i < N; i++)
        if (a[i] < N)
            b[a[i]] = min(b[a[i]], i);
    int t;
    cin >> t;
    while (t--)
    {
        int x;
        cin >> x;
        if (b[x] < 1e9)
            cout << b[x] << endl;
        else
            cout << -1 << endl;
    }
}

相关文章:

  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2021-04-07
猜你喜欢
  • 2022-01-05
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案