Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- bool pos(int n, int val)
- {
- int step;
- for(step=1;step<=val;step++)
- {
- n--;
- if(n%val!=0)
- return false;
- n=n-n/val;
- }
- return n%val==0;
- }
- int main()
- {
- int n,i,s,j,div[100009],flag;
- while(cin >> n)
- {
- if(n<0)
- break;
- flag=0;
- s=sqrt(n-1);
- for(i=2,j=0;i<=s;i++)
- {
- if((n-1)%i==0)
- {
- div[j++]=i;
- div[j++]=(n-1)/i;
- }
- }
- div[j++]=n-1;
- sort(div,div+j);
- for(i=j-1;i>=0;i--)
- {
- if(pos(n,div[i]))
- {
- printf("%d coconuts, %d people and 1 monkey\n",n,div[i]);
- flag=1;
- break;
- }
- }
- if(flag==0)
- printf("%d coconuts, no solution\n",n);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment