CF986F Oppa Funcan Style Remastered

给$n$与$k$,问是否能将n分为若干个$k$的因数之和

$k$分解质因数。

  • $k.size()=0$,$NO$.
  • $k.size()=1$,$(n\mod k = 0)$
  • $k.size()=2$,$ax+by=n$
  • $k.size()>2$,$ax+by+cz…=n$ 。

复杂度$O(50(\frac{\sqrt{k}}{\log}+k^{\frac{1}{3}}\log {k^{\frac{1}{3}}}))\approx 179056941$

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192


#include <bits/stdc++.h>
#include <bitset>
using namespace std;
typedef long long ll;
#define pii pair<ll, ll>
#define mk make_pair
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int M = 4e7 + 10;

bitset<40000100> npr;
int pr[M];
int pcnt;
void Prime_init(int X)
{
npr[1] = 1;

for (int i = 2; i <= X; i++)
{
if (!npr[i])
pr[++pcnt] = i;
for (int j = 1; j <= pcnt && pr[j] * i <= X; j++)
{
npr[pr[j] * i] = 1;
if (i % pr[j] == 0)
{
break;
}
}
}
}
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
{
x = 1, y = 0;
return a;
}
ll gd = exgcd(b, a % b, y, x);
y -= a / b * x;
return gd;
}
ll cinv(ll a, ll p)
{
ll x, y;
exgcd(a, p, x, y);
return (x + p) % p;
}

namespace SPFA
{
vector<pii> g[N];
ll dis[N];
int vis[N];
void spfa(int s, int c)
{
priority_queue<pii> q;
for (int i = 0; i < c; i++)
dis[i] = 2e18, vis[i] = 0;

q.push(mk(0, s));

vis[s] = 0;
dis[s] = 0;
while (!q.empty())
{
int x = q.top().second;
q.pop();
if (vis[x])
continue;
vis[x] = 1;
for (pii now : g[x])
{
int to = now.first;
int w = now.second;

if (dis[to] > dis[x] + w)
{
dis[to] = dis[x] + w;

q.push(mk(-dis[to], to));
}
}
}
}
} // namespace SPFA

using namespace SPFA;
int ans[N];
vector<int> p;

struct node
{
ll k, n, id;
} o[N];
bool cmp(node u, node v)
{
return u.k < v.k;
}
int main()
{

int T;
cin >> T;
for (int i = 1; i <= T; i++)
{
ll k, n;
cin >> n >> k;
o[i] = (node){k, n, i};
}
sort(1 + o, 1 + o + T, cmp);
vector<ll> p;
Prime_init(M - 100);
for (int i = 1; i <= T; i++)
{
if (o[i].k == o[i - 1].k)
{
if (p.size() == 0)
{
ans[o[i].id] = 0;
}
else if (p.size() == 1)
{
ans[o[i].id] = (o[i].n % p[0] == 0);
}
else if (p.size() == 2)
{
ll x = 1ll * o[i].n % p[1] * cinv(p[0], p[1]) % p[1];
ans[o[i].id] = (x * p[0] <= o[i].n);
}
else
{
ans[o[i].id] = (dis[o[i].n % p[0]] <= o[i].n);
}
}
else
{

ll k = o[i].k;
p.clear();
for (int i = 1; i <= pcnt && 1ll * pr[i] * pr[i] <= k; i++)
{

if (k % pr[i] == 0)
{

while (k % pr[i] == 0)
{
k /= pr[i];
}
p.push_back(pr[i]);
}
}

if (k > 1)
p.push_back(k);
if (p.size() == 0)
{
ans[o[i].id] = 0;
}
else if (p.size() == 1)
{
ans[o[i].id] = (o[i].n % p[0] == 0);
}
else if (p.size() == 2)
{
ll x = 1ll * o[i].n % p[1] * cinv(p[0], p[1]) % p[1];
ans[o[i].id] = (x * p[0] <= o[i].n);
}
else
{

ll c = p[0];
for (int j = 0; j < c; j++)
g[j].clear();
for (int j = 0; j < c; j++)
for (int i = 1; i < p.size(); i++)
{
g[j].push_back(mk((j + p[i]) % c, p[i]));
}
spfa(0, c);
ans[o[i].id] = (dis[o[i].n % c] <= o[i].n);
}
}
}
for (int i = 1; i <= T; i++)
{
puts(ans[i] ? "YES" : "NO");
}
}