193. Chinese Girls' Amusement
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: standard
output: standard
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.
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
Input file contains one integer number N (3 ≤ N ≤ 102000) - the number of Chinese girls taking part in the game.
Output
Output the only number - K that they should choose.
Sample test(s)
Input
Test #1
7
Test #2
6
7
Test #2
6
Output
3
Test #2
1
我们先研究怎样的K会满足要求,其实就是要求 k,2*k , 3*k , .... ,(n-1)*k 构成一个n的完全剩余系,
就是要求 a*k 不整除 n (对任意的1<=a<=(n-2) )
那么就是要求gcd(k,n) = 1;
如果每有K<=n/2 这一限制的话,我们很容易知道K=n-1 就是要求的值.
现在加了这一条件,其实问题没有变复杂多少。
1) 假设 n=2*m+1
有gcd(m,m+1) = m
直接得出k=m就是我们需要的解。
2)如果n=2*m
有gcd(m,m)=m && gcd(m-1,n) = gcd(m-1,m+1) = gcd(m-1,2)
(a) 如果m%2==1 有gcd(m-1,2) = 2 不符合条件 但是gcd(m-2 ,n) =gcd(m-2 ,m+2) =gcd(m-2,4) =1
故 k=m-2
(b) 如果m%2==0 有gcd(m-1,2) =1 那么答案就是 k=m-1
由于数据比较大,菜鸟又不会Java,只好手打了一个只能用于此题的大数。希望注意。
贴上代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)
using namespace std;
int const nMax = 2010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;
// std::ios::sync_with_stdio(false);
struct Int {
int a[nMax];
int l;
Int (int n=0) {
l=0;
while(n) {
a[l++]=n%10;
n/=10;
}
}
Int (char s[]){
l=strlen(s);
for(int i=l-1,j=0;i>=0;i--) a[j++]=s[i]-'0';
}
Int operator -- ( int ){
int d=1;
for(int i=0;i<l;i++){
a[i]=a[i]-d;
d=0;
if(a[i]<0){
d=1;
a[i]+=10;
}
}
while(a[l-1]==0&&l>0) l--;
return *this;
}
Int operator / (const int& b){
Int c(0);
int d=0;
for(int i=l-1;i>=0;i--){
d=d*10+a[i];
c.a[c.l++]=d/b;
d%=b;
}
reverse(c.a,c.a+c.l);
while(c.a[c.l-1]==0&&c.l>0) c.l--;
return c;
}
int operator % (const int &b) {
return a[0]%2;
}
void output(){
for(int i=l-1;i>=0;i--) printf("%d",a[i]);
printf("\n");
return ;
}
};
char s[nMax];
int main(){
scanf("%s",s);
Int a(s);
if(a%2==1) {
Int k=a/2;
k.output();
}else {
Int k=a/2;
k--;
if (k%2==0) {
k--;
}
k.output();
}
return 0;
}