题目链接:https://www.nowcoder.com/acm/contest/143/G

题目描述

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

case1

Input:

2 4

Output:

8

说明:

a=2,b=4

备注:

1<=c,n<=10^9

题目大意:

给定两个正整数 c,n,求一个数对 (a,b),满足 1<=a,b<=n,且 gcd(a,b)=c
要求输出最大的 ab
1<=c,n<=10^9

官方题解:

首先 a 和 b 一定都是 c 的倍数,如果 c<2n,那么选 a=b=c 最优
否则选 a=(n/c)*c , b=((n/c)-1)c

大概思路:

错误解法:一开始打了个素数筛,因为想着gcd(a, b) = c,所以 a, b等于 c 乘以 1 或者某两个素数,素数的范围到 N/c;用 set 把素数存起来,然后lower_bound( )  最接近N/c的素数,加一些判断分支。最后果断爆内存。

后来想一想,发现并不需要打素数表,其实脑洞一下有解的就是两种情况 N/c == 1,理想化的最大因子为1, 所以a = b = N/c(这里wa了几次); N/c >= 2 ,说明 a,b不等,c 分别乘以最大(N/c)和次大((N/c)-1)。

AC code:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long int
using namespace std; const int MAX_p = 1e8;
ll c, N, p; int main()
{
scanf("%lld%lld", &c, &N);
ll fmax_p = N/c, smax_p = ;
if(fmax_p >= )
{
if(fmax_p == ) printf("%lld\n", fmax_p*fmax_p*c*c);
else
{
smax_p = fmax_p-;
printf("%lld\n", smax_p*fmax_p*c*c);
}
}
else
{
printf("-1\n");
}
return ;
}

最新文章

  1. IOS开发之视图和视图控制器
  2. JS常用属性
  3. 深入分析Java Web技术内幕(修订版)
  4. PHP使用SnowFlake算法生成唯一ID
  5. 从客户端中检测到有潜在危险的request.form值
  6. javaweb学习总结二十五(response对象的用法一)
  7. pci hole -- 被吞噬的内存
  8. SQL注入专题
  9. HDU 4706 Children&#39;s Day(简单模拟)
  10. CentOS一般用户和root用户之间的切换
  11. Python Django 2.1登录功能_1
  12. linux入门--Linux系统的优缺点
  13. zabbix之 orabbix模板监控oracle
  14. CDHtmlDialog探索----WebBrowser扩展和网页Javascript错误处理
  15. Java面试集合(三)
  16. java实现word,ppt,excel,jpg转pdf
  17. Spring之Spring环境搭建
  18. Spark GraphX图处理编程实例
  19. Python全栈开发:list、元祖常用方法操作
  20. QT 简单 TCP 通信,发送数据到服务器

热门文章

  1. 带标准IO带缓存区和非标准IO 遇到fork是的情况分析
  2. (转)shell--read命令的选项及用法
  3. BSON入门
  4. 菜鸟学配置vim
  5. Java集合篇四:Map的基本应用
  6. 2018.10.23NOIP模拟赛解题报告
  7. 03_CronTrigger
  8. master.dbo.spt_values
  9. MySQL Database on Azure 支持 5.7 版本啦!
  10. 爬虫入门之jsonPath PhantomJS与 selenium详解(六)