Advertisement
jeff69

Untitled

Jul 3rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. typedef long double ld;
  6. int vis[100007];
  7. int main(){
  8.  
  9. queue<int> q;
  10. int n;
  11. cin>>n;
  12. q.push(n);
  13. memset(vis,-1,sizeof vis);
  14. vis[n]=0;
  15. while(!q.empty())
  16. {
  17.  
  18. int x=q.front();
  19. q.pop();
  20. for(int i=1;i*i<=x;i++)
  21. {
  22. int y=x-i*i;
  23. if(vis[y]!=-1)continue;
  24. vis[y]=vis[x]+1;
  25. q.push(y);
  26. if(y==0)
  27. {
  28. cout<<vis[y];
  29. return 0;
  30. }
  31.  
  32. }
  33. }
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement