题目描述:
You must have heard that the Chinese culture is quite different from that of Europe or Russia. So some Chinese habits seem quite unusual or even weird to us.
So it is known that there is one popular game of Chinese girls. N girls stand forming a circle and throw a ball to each other. First girl holding a ball throws it to the K-th girl on her left (1 ≤ K ≤ N/2). That girl catches the ball and in turn throws it to the K-th girl on her left, and so on. So the ball is passed from one girl to another until it comes back to the first girl. If for example N = 7 and K = 3, the girls receive the ball in the following order: 1, 4, 7, 3, 6, 2, 5, 1.
To make the game even more interesting the girls want to choose K as large as possible, but they want one condition to hold: each girl must own the ball during the game.
输入:
Input file contains multiple cases. Each case contains one integer number N (3 ≤ N ≤ 10^2000) - the number of Chinese girls taking part in the game.
输出:
Output the only number - K that they should choose for each input.
#include <bits/stdc++.h>
using namespace std;
/*
解题分析:高精度除法。
找到与N互质的最大整数K即可。
当N为奇数时,(N-1)/2即为所求数;
当N为偶数时,如果N/2 - 1是奇数,则为所求结果,如果为偶数,N/2 - 2为所求结果。
*/
// 商是C,余数是r
vector<int> div(vector<int> A, int b, int &r)
{
vector<int> C;
r = 0;
for(int i = A.size() - 1; i>=0 ;i--)
{
r = r * 10 + A[i];
C.push_back(r/b);
r %= b;
}
reverse(C.begin(),C.end());
while(C.size() > 1 && C.back()==0) C.pop_back();
return C;
}
// 减
vector<int> sub(vector <int> a,int b)
{
int i;
if(a[0] >= b) {
a[0] -= b;
return a;
}
for( i = 1; i < a.size();i++ )
{
if(a[i] != 0) {
a[i]--;
break;
}
}
for(int j = 1; j < i; j++)
{
a[j] = 9;
}
a[0] = a[0] + 10 - b;
while(a.size() > 1 && a.back()==0) a.pop_back();
return a;
}
int main()
{
string a;
while(cin >> a){
vector<int> A , res;
for(int i = a.size() - 1; i >= 0 ; i--) A.push_back(a[i] - '0');
int r;
if( A[0]%2 == 1 )
{
auto ans1 = sub(A,1);
res = div(ans1,2,r);
}
else
{
auto ans2 = div(A,2,r);
res = sub(ans2,1);
if(res[0]%2 == 0) res = sub(ans2,2);
}
for(int i = res.size() - 1;i >= 0; i--) printf("%d",res[i]);
puts("");
}
return 0;
}