题目大意
你有一个正整数nnn和一个大小为mmm的可重集BBB。
每次你可以可重集BBB中选择一个数xxx,将xxx变为⌊nx⌋\lfloor \dfrac nx\rfloor⌊xn⌋。
问通过上述操作,能将nnn变成多少种不同的数。
1≤n≤1015,1≤x≤1015,1≤m≤101\leq n\leq 10^{15},1\leq x\leq 10^{15},1\leq m\leq 101≤n≤1015,1≤x≤1015,1≤m≤10
时间限制1s1s1s。
题解
首先,我们知道,⌊nd⌋\lfloor \dfrac nd\rfloor⌊dn⌋的取值个数是O(n)O(\sqrt n)O(n)的。证明如下:
- 当1≤d≤n1\leq d\leq \sqrt n1≤d≤n时,最多有n\sqrt nn个ddd,也就是n\sqrt nn个⌊nx⌋\lfloor \dfrac nx\rfloor⌊xn⌋,故此时取值个数是O(n)O(\sqrt n)O(n)的
- 当n<d≤n\sqrt n<d\leq nn<d≤n时,1≤⌊nx⌋≤n1\leq \lfloor \dfrac nx\rfloor\leq \sqrt n1≤⌊xn⌋≤n,故此时取值个数是O(n)O(\sqrt n)O(n)的
所以,⌊nd⌋\lfloor \dfrac nd\rfloor⌊dn⌋的取值个数是O(n)O(\sqrt n)O(n)的。
直接搜索,然后用mapmapmap记录每个值是否被搜索过。不过,因为调用mapmapmap还要一个log\loglog的时间复杂度,所以我们考虑小于等于n\sqrt nn的数用一个数组标记,大于n\sqrt nn的数用mapmapmap标记。
时间复杂度为O(nlogn)O(\sqrt n\log n)O(nlogn)。
最坏情况下的数据如下:
1000000000000000 10
2 3 5 7 11 13 17 19 23 29
如果用上述方法来实现,这个数据只要跑不到500ms500ms500ms,所以是可以过的。
code
#include<bits/stdc++.h>
using namespace std;
int m;
long long n,ans=0,x[15];
bool z[33000005];
map<long long,bool>mp;
void dfs(long long t){
if(t<=sqrt(n)){
if(z[t]) return;
z[t]=1;
}
else{
if(mp[t]) return;
mp[t]=1;
}
++ans;
for(int i=1;i<=m;i++){
dfs(t/x[i]);
}
}
int main()
{
// freopen("set.in","r",stdin);
// freopen("set.out","w",stdout);
scanf("%lld%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%lld",&x[i]);
if(x[i]==1){
--i;--m;
}
}
sort(x+1,x+m+1);
m=unique(x+1,x+m+1)-x-1;
dfs(n);
printf("%lld",ans);
return 0;
}
文章讨论了一个关于整数n和大小为m的可重集B的计算问题,通过一系列操作将n变为其他数,分析了取值个数并提出了一种时间复杂度为O(n√nlogn)的解决方案。
2336

被折叠的 条评论
为什么被折叠?



