LOJ2803 CCC2018 平衡树 数论分块、记忆化搜索
题意差评,其实就是一个递推式:\(f_1 = 1 , f_i = \sum\limits_{j=2}^i f_{\lfloor \frac{i}{j} \rfloor}\),然后求\(f_N\)的值
首先\(\lfloor \frac{i}{j} \rfloor\)只有\(2\sqrt{i}\)种取值,这意味着每一次计算的关联量并不多
而每一次只需要求一组数据的解,于是大力记忆化搜索一下就可以AC了
PS:在Luogu AC此题有263分大礼包QAQ
#include<bits/stdc++.h>
#include<unordered_map>
#define ll long long
//This code is written by Itst
using namespace std;
unordered_map < int , ll > ans;
ll solve(int x){
if(x == 1) return 1;
if(ans.find(x) != ans.end()) return ans[x];
ll sum = 0;
for(int i = 2 , pi ; i <= x ; i = pi + 1){
pi = x / (x / i);
sum += solve(x / i) * (pi - i + 1);
}
return ans[x] = sum;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
int N;
cin >> N;
cout << solve(N);
return 0;
}