P1069

[NOIP2009 普及组] 细胞分裂

我们想了一想,相当于问 $s_i$ 的 $k$ 次幂是 $m_1^{m_2}$ 的倍数,这个最小的 $k$ 是多少。

显然就是对 $m_1$ 质因数分解,每个质数的次幂乘上 $m_2$ 就可以分解该数,然后再看每个质因子是否都是 $s_i$ 的质因子,如果不是,那么这个不可能有方案;如果是,那么取次幂的商的上取整的最大值作为 $s_i$ 的答案。

最后比较一个最小值即可。

时间复杂度 $O(\sqrt m_1+n\log m_1\log s)$。

代码:

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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<bitset>
#define ll long long
using namespace std;

const ll N=3e5;

ll n,m1,m2,cnt,ans,s;

ll f[N+5],g[N+5];

inline ll read() {
ll ret=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') {ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();}
return ret*f;
}

void write(ll x) {
static char buf[22];static ll len=-1;
if(x>=0) {
do{buf[++len]=x%10+48;x/=10;}while(x);
}
else {
putchar('-');
do{buf[++len]=-(x%10)+48;x/=10;}while(x);
}
while(len>=0) putchar(buf[len--]);
}

int main() {

n=read();

m1=read();m2=read();

for(ll i=2;i*i<=m1;i++) {
if(m1%i!=0) continue;
f[++cnt]=i;
while(m1%f[cnt]==0) {
g[cnt]++;m1/=f[cnt];
}
g[cnt]*=m2;
}
if(m1>1) {f[++cnt]=m1;g[cnt]=m2;}

ans=-1;
for(ll i=1;i<=n;i++) {
s=read();ll tmp=0;
for(ll j=1;j<=cnt;j++) {
if(s%f[j]!=0) {tmp=-1;break;}
ll tot=0;
while(s%f[j]==0) {
tot++;s/=f[j];
}
tmp=max(tmp,(g[j]+tot-1)/tot);
}
if(tmp==-1) continue;
if(ans==-1) ans=tmp;
else ans=min(ans,tmp);
}

write(ans);

return 0;
}