Chinese Girls' Amusement (大数除法,大数减法)

本文介绍了一个关于中国女孩间流传的游戏算法问题。该算法关注如何确定最大整数值K,使得在N个参与者中进行的传球游戏中,每个参与者都能恰好接收到一次球。文章通过分析得出了解决方案,并提供了一段C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值