Guest User

Untitled

a guest
Jan 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cmath>
  5. using namespace std;
  6.  
  7. bool pos(int n, int val)
  8. {
  9. int step;
  10. for(step=1;step<=val;step++)
  11. {
  12. n--;
  13. if(n%val!=0)
  14. return false;
  15. n=n-n/val;
  16. }
  17. return n%val==0;
  18.  
  19. }
  20.  
  21. int main()
  22. {
  23. int n,i,s,j,div[100009],flag;
  24. while(cin >> n)
  25. {
  26. if(n<0)
  27. break;
  28. flag=0;
  29. s=sqrt(n-1);
  30. for(i=2,j=0;i<=s;i++)
  31. {
  32. if((n-1)%i==0)
  33. {
  34. div[j++]=i;
  35. div[j++]=(n-1)/i;
  36. }
  37. }
  38. div[j++]=n-1;
  39. sort(div,div+j);
  40. for(i=j-1;i>=0;i--)
  41. {
  42. if(pos(n,div[i]))
  43. {
  44. printf("%d coconuts, %d people and 1 monkey\n",n,div[i]);
  45. flag=1;
  46. break;
  47. }
  48.  
  49. }
  50. if(flag==0)
  51. printf("%d coconuts, no solution\n",n);
  52. }
  53. return 0;
  54. }
Add Comment
Please, Sign In to add comment