Chinese Girls' Amusement (大數除法,大數減法)

Chinese Girls' Amusement


Time limit      2000 ms                Memory limit         65536 kB

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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.



Input

Input file 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

2

7

6


Sample Output

3

1



題解:題意爲n個人圍成一圈,編號爲1~n,從1開始,拋繡球給1左邊第k個人(1<=k<=n/2),直到繡球再次回到1的手中,條件:每個人必須扔一次繡球,問符合條件的k的最大值……

我首先模擬了一下,發現一個規律:

1:n%2==0         

                                    a:n/2%2==0        輸出   n/2-1

                                    b:n/2%2==1        輸出   n/2-2

2:n%2==1       

                        輸出      n/2

大數除法一遍過,就那個減法讓我錯了好幾次,笑哭……



#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<string>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define ll __int64
#define For(i,a,b) for(int i=a;i<b;i++)
#define sf(a)  scanf("%d",&a)
#define sfs(a)  scanf("%s",a)
#define sff(a,b)  scanf("%d%d",&a,&b)
#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pf(a) printf("%d\n",a)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int a[2005],b[2005];
char x[2005];
int main()
{
    int t,s=0;
    sf(t);
    while(t--)
    {
        if(s)printf("\n");s=1;
        mem(a,0);
        mem(b,0);
        memset(x,'\0',sizeof(x));
        scanf("%s",x);
        int l=strlen(x);
        for(int i=0; i<l; i++)
        {
            a[i]=x[i]-'0';
        }
        if(a[l-1]%2==0)
        {
            int d=0;
            for(int i=0; i<l; i++)
            {
                b[i]=(d*10+a[i])/2;
                d=a[i]%2;
            }
            if(b[l-1]%2==0)
            {
                mem(a,0);
                int d=1;
                for(int i=l-1; i>=0; i--)
                {
                    a[i]+=b[i]-d;
                    d=0;
                    if(a[i]<0)
                    {
                        a[i]+=10;
                        b[i-1]--;
                    }
                }
                int flag=0;
                for(int i=0; i<l; i++)
                    if(a[i])
                    {
                        flag=i;
                        break;
                    }
                for(int i=flag; i<l; i++)printf("%d",a[i]);
                printf("\n");
            }
            else
            {
                mem(a,0);
                int d=2;
                for(int i=l-1; i>=0; i--)
                {
                    a[i]+=b[i]-d;
                    d=0;
                    if(a[i]<0)
                    {
                        a[i]+=10;
                        b[i-1]--;
                    }
                }
                int flag=0;
                for(int i=0; i<l; i++)
                    if(a[i])
                    {
                        flag=i;
                        break;
                    }
                for(int i=flag; i<l; i++)printf("%d",a[i]);
                printf("\n");
            }
        }
        else
        {
            int d=0;
            for(int i=0; i<l; i++)
            {
                b[i]=(d*10+a[i])/2;
                d=a[i]%2;
            }
            int flag=0;
            for(int i=0; i<l; i++)
                if(b[i])
                {
                    flag=i;
                    break;
                }
            for(int i=flag; i<l; i++)printf("%d",b[i]);
            printf("\n");
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章