Advertisement
a53

VectorDiv

a53
Feb 28th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<int> v,st,d;
  4. vector<vector<int>> vd;
  5. int n,res(1e9),curr;
  6.  
  7. inline void Back(int k)
  8. {
  9. if(k==0&&res>curr)
  10. res=curr;
  11. if(curr>=res)
  12. return;
  13. for(const int& x:vd[st[k+1]])
  14. {
  15. st[k]=x;
  16. curr+=abs(st[k]-v[k]);
  17. Back(k-1);
  18. curr-=abs(st[k]-v[k]);
  19. }
  20. }
  21. int main()
  22. {
  23. cin>>n;
  24. v=st=vector<int>(n+1);
  25. for(int i=1;i<=n;++i)
  26. cin>>v[i];
  27. for(int i=1;i<=v[n];++i)
  28. if(v[n]%i==0)
  29. d.emplace_back(i);
  30. vd=vector<vector<int>>(v[n]+1);
  31. for(size_t i=0;i<d.size();++i)
  32. for(size_t j=i;j<d.size();++j)
  33. if(d[j]%d[i]==0)
  34. vd[d[j]].emplace_back(d[i]);
  35. st[n]=v[n];
  36. Back(n-1);
  37. cout<<res;
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement