牛客网暑期ACM多校训练营(第五场).plan(模拟)

链接:https://www.nowcoder.com/acm/contest/143/G
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Give two positive integer c, n. You need to find a pair of integer (a,b) satisfy 1<=a,b<=n and the greatest common division of a and b is c.And you need to maximize the product of a and b

输入描述:

The first line has two positive integer c,n

输出描述:

Output the maximum product of a and b.

If there are no such a and b, just output -1

示例1

输入

复制

2 4

输出

复制

8

说明

a=2,b=4

备注:

1<=c,n<=10^9

 解题思路:大部分房间按照性价比最高的分配,最后剩的人数讨论一下就行。

参考代码:

#include<iostream>
using namespace std;
long long minn( long long a,long long b){
    return (a<=b)?a:b;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long n,p1,p2;
    while(cin>>n>>p1>>p2)
    {
        long long k1=p1*3,k2=p2*2;
        long long a=-1,ans=0,m=n/6;
        if(k1<=k2)
            ans+=m*3*p1;
        else
            ans+=m*2*p2;
        n%=6;
        if(n==1)
        {
            if(m){
                if(k1<=k2)
                {
                    ans-=3*p1;
                    a=minn(3*p2,4*p1);
                    a=minn(a,2*p2+p1);
                    a=minn(a,2*p1+p2);
                    ans+=a;
                }else{
                    ans-=2*p2;
                    a=minn(3*p2,4*p1);
                    a=minn(a,2*p2+p1);
                    a=minn(a,2*p1+p2);
                    ans+=a;
                }
            }
            else ans+=minn(p1,p2);
        }
        if(n==2) ans+=minn(p1,p2);
        if(n==3) ans+=minn(2*p1,p2);
        if(n==4) ans+=minn(2*p1,2*p2);
        if(n==5)
        {
            a=minn(p1+p2,2*p2);
            a=minn(a,3*p1);
            ans+=a;
        }
        cout<<ans<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/XxxxxM1/article/details/81390080
今日推荐