牛客-max

鏈接: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

題解:題目要求找出n範圍內以c爲最大公約數的兩個數字,在n內最大以c爲因子的數b=n/c*c;

又因爲兩個相鄰的數字乘以一個數後最大公約數一定是那個數,例如,2*c和3*c或者5*c和6*c,所以另一個數a=b-c;

最後注意特例,當b==c時,a=c,當n<c時輸出-1;

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
ll c,n;
cin>>c>>n;
ll a,b;

b=n/c*c;
if(b!=c)
a=b-c;
else
a=c;

if(c<n)
cout<<a*b<<endl;
else if(c==n)
    cout<<c*c<<endl;
else
     cout<<"-1"<<endl;
    return 0;
}

 

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