Codeforces Round #713 (Div. 3)
题意
定义:d(x) 为 x 的所有因子和。eg : d(6) = 1 + 2 + 3 + 6 = 12。
有多次询问,每次查询给出一个数 c,输出满足 d(x) = c 的最小 x,如果不存在输出 -1。
码1
线性筛法 O ( n ) O(n) O(n)
- 若 g c d ( a , b ) = 1 gcd(a, b) = 1 gcd(a,b)=1, d ( a b ) = d ( a ) × d ( b ) d(ab) = d(a) \times d(b) d(ab)=d(a)×d(b)
- 若
g
c
d
(
a
,
b
)
=
q
1
,
q
1
>
1
gcd(a, b) = q_1, q_1 > 1
gcd(a,b)=q1,q1>1,
d ( a ) = ( 1 + q 1 + q 1 2 + ⋯ + q 1 c 1 ) × ( 1 + q 2 + q 2 2 + ⋯ + q 2 c 2 ) × ( 1 + q m + q m 2 + ⋯ + q m c m ) {d}(a) = (1 + q_1 + q_1^2 + \cdots + q_1^{c_1}) \times (1 + q_2 + q_2^2 + \cdots + q_2^{c_2}) \times (1 + q_m + q_m^2 + \cdots + q_m^{c_m}) d(a)=(1+q1+q12+⋯+q1c1)×(1+q2+q22+⋯+q2c2)×(1+qm+qm2+⋯+qmcm)
d ( b ) = d ( a q ) = ( 1 + q 1 + q 1 2 + ⋯ + q 1 c 1 + q 1 c 1 + 1 ) × ( 1 + q 2 + q 2 2 + ⋯ + q 2 c 2 ) × ( 1 + q m + q m 2 + ⋯ + q m c m ) {d}(b) = {d}(aq) = (1 + q_1 + q_1^2 + \cdots + q_1^{c_1} + q_1^{c_1 + 1}) \times (1 + q_2 + q_2^2 + \cdots + q_2^{c_2}) \times (1 + q_m + q_m^2 + \cdots + q_m^{c_m}) d(b)=d(aq)=(1+q1+q12+⋯+q1c1+q1c1+1)×(1+q2+q22+⋯+q2c2)×(1+qm+qm2+⋯+qmcm) - 按照线性筛的思路,每个数只会被自身最小的质因数筛一遍的思路去编写即可。但需要设置一个辅助数(mul)组来存最小的质因数的等比和,也即上面推导的一个括号里的内容。可以发现
- a为质数,mul(a) = a + 1
- m u l ( b ) = m u l ( a ) ∗ q 1 + 1 mul(b) = mul(a) * q_1 + 1 mul(b)=mul(a)∗q1+1
- 综上: d ( b ) = { b + 1 b是素数 d ( a ) / m u l ( a ) × m u l ( ( b ) b是合数, gcd(a, b) > 1 d(b)=\begin{cases} b + 1& \text{b是素数}\\{d}(a) / mul(a) \times mul((b)& \text{b是合数, gcd(a, b) > 1} \end{cases} d(b)={b+1d(a)/mul(a)×mul((b)b是素数b是合数, gcd(a, b) > 1
#include<cstdio>
#include<queue>
#include<set>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<unordered_map>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define ull unsigned long long int
#define mt(a,b) memset(a, b, sizeof a)
//#define int long long
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 1e5 + 7, M = 1e7 + 2;
int d[M], mul[M], mp[M];//mp为答案数组
int v[M], p[M], m;//p素数,v记录最小质因子
void init()//线性筛
{
d[1] = 1;
m = 0;
for (int i = 2; i < M; i++)
{
if (v[i] == 0)//i为质数
{
v[i] = i;
p[++m] = i;
mul[i] = i + 1;
d[i] = i + 1;
}
for (int j = 1; j <= m; j++)
{
if (p[j] > v[i] || p[j] > M / i) break;
v[i * p[j]] = p[j];
if (i % p[j] == 0)//若i为合数,或者i = p[j]时
{
mul[i * p[j]] = mul[i] * p[j] + 1;
d[i * p[j]] = d[i] / mul[i] * mul[i * p[j]];
}
else
{
mul[i * p[j]] = p[j] + 1;//最小的质因数为p[j]
d[i * p[j]] = d[i] * d[p[j]];
}
}
}
}
int main()
{
IOS;
init();
for (int i = 1; i < M; i++)
{
if (d[i] < M && mp[d[i]] == 0) mp[d[i]] = i;
//cout<< i << "--" << d[i] << endl;
}
int T; cin >> T;
while (T--)
{
int c; cin >> c;
if (mp[c]) cout << mp[c] << endl;
else cout << -1 << endl;
}
return 0;
}
码2
埃氏筛法
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
明显码2代码短小,这可能就是所谓的“牺牲空间换取时间”吧
#include<bits/stdc++.h>
#include<cstdio>
#include<queue>
#include<set>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define ull unsigned long long int
#define mt(a,b) memset(a, b, sizeof a)
//#define int long long
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 2e5 + 7, M = 1e7 + 10;
int d[M], mp[M];
int main()
{
IOS;
for (int i = 1; i <= M - 5; i++)
for (int j = i; j <= M - 5; j += i)
d[j] += i;
for (int i = 1; i <= M - 5; i++)
if (d[i] < M && mp[d[i]] == 0) mp[d[i]] = i;
int T; cin >> T;
while (T--)
{
ll c; cin >> c;
if (!mp[c]) cout << -1 << endl;
else cout << mp[c] << endl;
}
return 0;
}