链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
时间限制: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
题意
gcd(a,b)=c 1<a,b<n 求max(a*b)
输入描述:第一行有两个正整数(c,n)
输出描述:输出a和b的最大乘积。如果没有这样的a和b,就输出-1
题解
gcd(a,b)=c,则a*b=c*c*x*y。x和c互质,相邻的两个大于2的正整数一定互质。p=n/c,所以p*(p-1)最大。特判:p=0,则不存在。p=1,则将最大的数平方。
AC代码
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include <iomanip>
#define rep(i,a,n) for(int i=1;i<=n;i++)
#define per(I,a,n) for(int i=n;i>=a;i--)
using namespace std;
typedef long long ll;
const int mod=1000000007;
const int maxn=2e4;
int main(int argc, const char * argv[]) {
ll c,n,p;
cin>>c>>n;
p=n/c;
if(p==0)
cout<<-1<<endl;
else if(p==1)
cout<<p*c*p*c<<endl;
else
cout<<p*c*(p-1)*c<<endl;
return 0;
}