第九届山东理工大学ACM网络编程擂台赛 正式赛 sdut4075GCD - ldq的黑心啤酒厂

题目链接
ldq’s brewery
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

LDQ has a black heart brewery, and in order to get people to buy a beer, he designs that a bottle of wine could just pour seven cups. Since we often have a toast at the party, we have to fill each other with a toast.

When there are two people , there will have three rounds, exactly one more cup, When there are three people, there will have two rounds, exactly one more cup; and four people will have three more cups.

In the above case, in order to obey the principle of not wasting, you will buy a bottle of beer and do another round. Of course, there’s probably a lot beer to buy… Then the cycle repeats and drinks a lot. Until the beer has just finished.

Now LDQ has designed that the bottle could just pour the x cup, please find two people, three people, and until n people at the party, how many bottles of beer each brewery can sell .
Input

There are several sets of test data.
First line contains an integer T, which indicates the number of test cases.
Then t lines follow, each of these lines contain two integers x and n. ( 1<=x<=10^6, 2<=n<=10^6 )
Output

For every test case, you should output n-1 lines, which respectively indicates the number of beer bottles when 2,3,4,……,n people that take part in the party could sell.
Example Input

1
7 5
Example Output

2
3
4
5
Hint

Author

axuhongbo

中文题面:
ldq有一个黑心啤酒厂,他为了让大家买啤酒,会把一瓶酒设计成恰好能倒七杯。由于聚会时经常会有大家一起干杯这样的事情,干杯之前又要给每个人都倒满,所以来两个人的时候,干完三轮,恰好多一杯;三个人的时候,干完两轮,恰好多一杯;四个人的时候会多三杯。在上述情况下,为了践行不浪费的原则,就会多买一瓶啤酒,再干一轮。当然多买的啤酒可能又有多了……然后循环往复,喝了好多好多。直到啤酒刚刚好喝完为止。

现在ldq把酒瓶设计成刚好能倒x杯,请依次求出有 2人,3人,一直到n人参加聚会时,啤酒厂各能卖出多少瓶啤酒。

输入

首先输入一个T,代表测试数据

接着输入T行,每行有两个整数x和n。1<=x<=10^6,2<=n<=10^6

输出
对于每组数据输出 n-1行,分别表示2,3,4……n 人参加聚会时,能卖出的啤酒瓶数

示例输入
1
7 5

示例输出
2
3
4
5

。                      Accepted Total   Ratio
E [4075] ldq's brewery  24      56         42.9%

思路:
其实是一道很水的题目,但是由于是英文题面,所以可能会有人都不懂题意。最后的通过率还可以。
一瓶酒倒x杯,y个人参加聚会,要想答案是整瓶,找一个能既能除尽x的数,也能除尽y的数,即求x与y的最小公倍数,然后除以x就可以了。要求x,y的最小公倍数,先求其最大公约数gcd,然后最小公倍数就是 (x*y)/gcd(x,y).下面求gcd的部分写法非常标准,希望不会的小伙伴可以认真学习一下。

#include<bits/stdc++.h>
//万能头文件,具体作用自行百度
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    int x,n;
    while(t--)
    {
        scanf("%d%d",&x,&n);
        for(int i=2; i<=n; i++)
        {
            int a=x,b=i,c;//求gcd的部分
            while(b)
            {
                c = a%b;
                a = b;
                b = c;
            }
            //a为求出的x,i的最大公约数
            printf("%d\n",i/a);
        }
    }
    return 0;
}

正式比赛中由于题目数据范围缩小,所以大多数人的暴力做法也能水过这道题,但是还是希望能够采用标准做法。
暴力代码示例:

#include<cstdio>
int main()
{
    int T,x,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&x,&n);
        for(int a=2; a<=n; a++)
        {
            int j=1;
            while(x*j%a!=0)
                j++;
            printf("%d\n",j);
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值