ACdream 1210 Chinese Girls' Amusement(高精度)
Description
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 contains one integer number N (3 ≤ N ≤ 10 2000) — the number of Chinese girls taking part in the game.
Output
Output the only number — K that they should choose.
Sample Input
7 6
Sample Output
3 1
题意:给定一个很大的数N,求一个数K,K大于等于1小于等于N/2,使得gcd(N,K)=1。
题解:打表可以发现规律。
打表可以发现规律 奇数就是除以2 偶数如果是4的倍数则除以2减1 偶数如果非4的倍数则除以2减2
接下来就是代码的实现了,用字符串存储数组。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int main() { int a[2050],b[2050]; char c[2050]; while(scanf("%s",c)!=EOF) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); int len=strlen(c); int cnt=0; for(int i=0; i<len; i++) a[i]=c[i]-'0'; for(int i=0; i<len; i++) { b[i]=(a[i]+cnt*10)/2; if(a[i]%2==1) cnt=1; else cnt=0; } int jian=0; int sum=a[len-2]*10+a[len-1]; //奇数就是除以2 偶数4的倍数除以2减1 偶数非4的倍数除以2减2 if(sum%2==0) { if(sum%4==0) jian=1; else jian=2; } int sumb=0,k=0; if(jian==1) { int flag1=0; for(int i=len-1; i>=0; i--) { if(b[i]==0) { flag1=1; continue; } else { k=i; for(int j=k;j<len-1;j++) b[j]--; if(flag1) b[len-1]=9; else b[len-1]--; break; } } } else if(jian==2) //11 { int flag=0; int flag2=0; if(b[len-1]>=2) //这个和上面的len-1不一样 { b[len-1]-=2; flag=1; flag2=1; } for(int i=len-2; i>=0; i--) //最后一位肯定不行的 { if(flag) break; if(b[i]==0) { flag2=1; continue; } else {//11 k=i; for(int j=k;j<len-1;j++) b[j]--; b[len-1]=b[len-1]+10-2; break; } } } int j=0; while(b[j]==0) j++; for(; j<len; j++) printf("%d",b[j]); printf("\n"); } return 0; }