Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int curOper[1000001];
  6. vector<int> ans;
  7.  
  8. void makeAns(int n)
  9. {
  10. ans.push_back(n);
  11. if (n==1) return;
  12. int mn=1e9,numOfOper;
  13. if (mn>curOper[n-1]) mn=curOper[n-1],numOfOper=1;
  14. if (n%2==0 && mn>curOper[n/2]) mn=curOper[n/2],numOfOper=2;
  15. if (n%3==0 && mn>curOper[n/3]) mn=curOper[n/3],numOfOper=3;
  16. if (numOfOper==1) makeAns(n-1);
  17. if (numOfOper==2) makeAns(n/2);
  18. if (numOfOper==3) makeAns(n/3);
  19. }
  20.  
  21. int main() {
  22. int n;
  23. cin>>n;
  24. memset(curOper,0,sizeof(curOper));
  25. if (n==1) return cout<<"0\n1",0;
  26. for (int i=2;i<=n;i++)
  27. {
  28. curOper[i]=curOper[i-1]+1;
  29. if (i%2==0 && curOper[i/2]+1<curOper[i])
  30. curOper[i]=curOper[i/2]+1;
  31. if (i%3==0 && curOper[i/3]+1<curOper[i])
  32. curOper[i]=curOper[i/3]+1;
  33. }
  34. cout<<curOper[n]<<endl;
  35. curOper[0]=1e9;
  36. makeAns(n);
  37. reverse(ans.begin(),ans.end());
  38. for (int i=0;i<ans.size();i++)
  39. cout<<ans[i]<<' ';
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement