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: 由于 ...
随机推荐
- tensorflow源码分析——CTC
CTC是2006年的论文Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurren ...
- 【Java基础】内部类
非静态内部类不能拥有静态变量 为什么 下面这段代码,如果Lazyholder没有static修饰,则编译不过 class Singleton2 { private static class LazyH ...
- jupyter 服务器安装随笔
python3:python3 -m pip install --upgrade pip python3 -m pip install jupyterpkg install py36-pyzmq-18 ...
- c++ 调用 sqlcipher
#include <iostream> #include <string.h> #include "sqlite3.h" using namespace s ...
- jmeter函数介绍(1)
1._Random() 随机数 The minimum value allowed for a range of values:输入一个最小值: The maximum value allowed f ...
- LeetCode链表简单题
一.21合并两个有序链表 代码如下: class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNod ...
- Sql注入校验
/// <summary> /// Sql注入校验 /// </summary> /// <param name="listWord">字符&l ...
- docker安装tomcat&部署javaweb程序
一.docker定制简单的java-web应用镜像 网址: 1.jdk下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- python面试题--初级(二)
基础不牢,地动山摇,面试的时候经常会被问到一些平时基础的很容易被忽视的知识点,所以重在积累,多看多背深入理解,才能在某一天工作中豁然开朗恍然大悟. 面试题不仅仅为了应付面试,更是知识点的一个梳理总结归 ...
- MySql日期加天数,小时,分钟...得到新的时间
在当前的日期上加三天,天数随便改: SELECT date_add(CURRENT_DATE(), interval 3 day); 在指定的日期上加三天: SELECT date_add('2014 ...