CSP 校门外的树 2021-4 C++解法
用动态规划时间复杂度
O
(
N
2
)
O(N^2)
O(N2)
代码
#include<iostream>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const ll MAXN=100000;
const ll mod=1e9+7;
class Solution{
private:
vector<ll>::iterator it;
set<ll> st;
vector<vector<ll>> f=vector<vector<ll>>(MAXN);
public:
void fact(){
for(ll i=1;i<=MAXN/2;i++){
for(ll j=i*2;j<=MAXN;j+=i){
f[j].emplace_back(i);
}
}
}
ll solve(ll left,ll right){
ll len=right-left;
ll res=0;
st.insert(len);
for(it=f[len].begin();it!=f[len].end();it++){
if(st.find(*it)!=st.end())
continue;
res++;
st.insert(*it);
}
return res;
}
int getAllCount(int n,vector<int> &a){
vector<int> dp(1005);
fact();
dp[0]=1;
for(int i=1;i<n;i++){
st.clear();
for(int j=i-1;j>=0;j--){
dp[i]+=(dp[j]*solve(a[j],a[i]))%mod;
dp[i]%=mod;
}
}
return dp[n-1];
}
};
int main(){
Solution solution;
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
int res=solution.getAllCount(n,a);
cout<<res<<endl;
}
|