完全不理解这题出的啥目的,反正按他说的做吧,模拟下判断就OK了,莫名其妙
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
namespace
{
bool can(int n, int p)
{
for (int i = 0; i < p; i++)
if ((n - 1) % p == 0)
n = (n - 1) / p * (p - 1);
else
return false;
return n % p == 0;
}
}
int main()
{
int n;
vector<int> V;
while (scanf("%d", &n), n != -1)
{
V.clear();
for (int i = 1; i * i <= n - 1; i++)
if ((n - 1) % i == 0)
{
V.push_back(i);
V.push_back((n - 1) / i);
}
sort(V.begin(), V.end());
reverse(V.begin(), V.end());
int people = -1;
for (size_t t = 0; t < V.size() - 1; t++)
if (can(n, V[t]))
{
people = V[t];
break;
}
if (people != -1)
printf("%d coconuts, %d people and 1 monkey\n", n, people);
else
printf("%d coconuts, no solution\n", n);
}
return 0;
}