Uva 616 Solution

#include<bits/stdc++.h> #define ll long long int using namespace std; int main() { ll n, m, i, j; while(cin >> n && n != -1) { int p = 0; if(n == 3) { cout << "3 coconuts, 2 people and 1 monkey" << endl; continue; } m = sqrt(n); for(i=m;i>=2;i--) { ll t = n; for(j=1;j<=i;j++) { if(t%i == 0 || t%i != 1) break; else t = (t-1)- (t-1)/i; } if(t%i == 0 && j-1 == i) { p = 1; break; } } if(p == 1) cout << n << " coconuts, "<< i << " people and 1 monkey" << endl; else cout << n << " coconuts, no solution" << endl; } return 0; }

Comments