2019长安大学ACM校赛网络同步赛 M LCM (数论)
链接:https://ac.nowcoder.com/acm/contest/897/M
来源:牛客网
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
Give two positive integers n and c. Find a pair of positive integer (a, b), which satisfies both of a and b are no more than n and the lowest common multiple of them is c. Moreover, maximize a×ba×b, the product of a and b.
Please tell Silly Slp the maximize value of a×ba×b. If a and b don't exist, print “-1”(without quotes).
输入描述:
The first line contains an integer number T, the number of test cases.
ithith of each next T lines contains two numbers n and c(1≤n,c≤1061≤n,c≤106).
输出描述:
For each test case print a number, the maximize value.
输出
24
-1 题意:
给你一个n和一个c,
让你找两个数a,b,满足 a<=n,b<=n, lcm(a,b)=c
思路:
c=lcm(a,b)=a*b/gcd(a,b);
so, gcd(a,b) 是c的一个因子,
那么我们sqrt(c)的时间复杂度来枚举 c的因子i,我们假定i就是 gcd ,然后y=c/i
y=a*b/gcd(a,b)/gcd(a,b)
我们再来枚举 y的因子,如果y有两个因子 p,q, 满足gcd(p,q)==1 并且 p*gcd<=n,q*gcd<=n
那么 p,q就可以是 a/gcd(a,b), b/gcd(a,b)的一种可能,
p*gcd*q*gcd = a*b
我们枚举的时候取最大值,就可以得到我们想要的答案。 细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
int t;
// gbtb;
// cin>>t;
scanf("%d", &t);
while (t--)
{
ll n, c;
scanf("%lld %lld", &n, &c);
// cin>>n>>c;
ll res = -;
for (ll i = ; i * i <= c; i++)
{
if (c % i == )
{ ll y = c / i; //枚举gcd(a,b)=i a*b=c*i
for (ll j = ; j * j <= y; j++) {
if (y % j == && gcd(j, y / j) == && j * i <= n && y / j * i <= n) {
res = max(res, c * i);
}
}
y = i; //枚举gcd(a,b)=c/i a*b=c*(c/i)
for (ll j = ; j * j <= y; j++) {
if (y % j == && gcd(j, y / j) == && j * (c / i) <= n && y / j * (c / i) <= n) {
res = max(res, c * (c / i));
}
}
}
}
printf("%lld\n", res);
// cout<<ans<<endl;
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
2019长安大学ACM校赛网络同步赛 M LCM (数论)的更多相关文章
- 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)
链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)
链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- 2019长安大学ACM校赛网络同步赛C LaTale (树上DP)
链接:https://ac.nowcoder.com/acm/contest/897/C来源:牛客网 LaTale 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语 ...
- 2019长安大学ACM校赛网络同步赛 B Trial of Devil (递归)
链接:https://ac.nowcoder.com/acm/contest/897/B来源:牛客网 Trial of Devil 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...
- 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 I
链接:https://www.nowcoder.com/acm/contest/122/I来源:牛客网 题目描述 小q最近在做一个项目,其中涉及到了一个计时器的使用,但是笨笨的小q却犯难了,他想请你帮 ...
- NOI 2018网络同步赛(游记?)
刚中考完那段时间比较无聊,报名了一个同步赛,报完名才发现成绩单是要挂到网上的,而且因为报的早给了一个很靠前的考号...那布星啊,赶紧学点东西,于是在一周内学了网络流,Treap以及一些数论. Day1 ...
- 第十三届北航程序设计竞赛决赛网络同步赛 B题 校赛签到(建树 + 打标记)
题目链接 校赛签到 对每个操作之间建立关系. 比较正常的是前$3$种操作,若第$i$个操作属于前$3$种,那么就从操作$i-1$向$i$连一条有向边. 比较特殊的是第$4$种操作,若第$i$个操作属 ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- $NOI$ $2019$ 网络同步赛
D2T1考试前测了自己造的“假”极限数据,看了看内存发现是118MB,感觉没问题就交上去了. 赛后用Lemon测了一下:MLE 88->0 对于别的题我已经不想再说什么了. update: 由于 ...
随机推荐
- 《Effective Java》读书笔记 - 6.枚举和注解
Chapter 6 Enums and Annotations Item 30: Use enums instead of int constants Enum类型无非也是个普通的class,所以你可 ...
- 彩色点云生成mesh的纹理
上一篇文章 https://www.cnblogs.com/lovebay/p/11423576.html ,我们使用MPA算法实现了 点云生成mesh,但仅仅实现mesh的顶点着色,为了让mesh有 ...
- collection:指定要遍历的集合
//查询员工id'在给定集合中(1,6)的 public List<Employee> getEmpsByConditionForeach(@Param("ids")L ...
- 二、Python基础
1.变量名 数字,字母,下划线:aaa1;aa_b1 不能以数字开头:1aa 变量名不能是python内部的关键字 2.getpass import getpass username=raw_inpu ...
- 纯css闪烁效果
.i-i-box-active{ -webkit-animation: shan666 1s infinite ease-in-out; animation: shan666 1s infinite ...
- 002-Django数据库及后台admin配置
连接mysql数据库 数据库准备 如果连接本机数据库,mysql安装及配置可参考https://www.cnblogs.com/feizisy/p/11882521.html 如果连接阿里云RDS,需 ...
- Jmeter 后置处理器--jp@gc - JSON/YAML Path Extractor & JSON Extractor
后置处理器--jp@gc - JSON/YAML Path Extractor 1.需要下载插件,地址: 解压后把对应jar包放置对应的lib和lib/ext目录下,重启Jmeter: 2.在需要提取 ...
- 添加linux中svn的用户和密码
1:首先找到svn路径 find / -iname "svn" 一般找到svn路径之后就可以找到配置文件位置啦 svn/svnrepos/jgcp/conf 2:进入目录之后修改a ...
- 一个简单的dns服务器
options { listen-on port 53 { any; }; listen-on-v6 port 53 { any; }; directory "/srv/app/named& ...
- Scrapy输出文件格式问题汇总
Q:Scrapy抓取的内容(包含中文)输出到JSON Lines文件时如何确保输出的是字符本身而不是其unicode编码? A:默认的JsonLinesItemExporter其ensure_asci ...