列表

详情


NC17413. max

描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 484K, 提交时间: 2018-08-02 12:06:39

#include<cstdio>
int main(){
	long long a,b,ans;
	scanf("%lld%lld",&a,&b);
	if(a>b) return puts("-1"),0;
	b/=a;
	ans=a*a;
	ans*=b;
	if(b-1) ans*=(b-1);
	printf("%lld\n",ans);
}

C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 528K, 提交时间: 2018-08-04 19:01:07

#include<iostream>
using namespace std;
int main(){
	long long a,b,c;
	cin>>a>>b;
	c=b/a-1;
	if(!c) cout<<a*a;
	else if(c>0) cout<<a*a*c*(c+1);
	else cout<<-1;
}

Python3(3.5.2) 解法, 执行用时: 34ms, 内存消耗: 3392K, 提交时间: 2018-08-02 12:12:16

x, y = map(int, input().split())
y //= x
print (-1 if y == 0 else max(1, y * (y - 1)) * x * x)

pypy3 解法, 执行用时: 114ms, 内存消耗: 25840K, 提交时间: 2021-10-05 17:54:38

c,n=map(int,input().split())
print("-1") if n<c else print(n//c*max(1,n//c-1)*c*c)

上一题