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: 由于 ...
随机推荐
- ES6注
1.Promise构造函数 //resolve(成功),reject(失败)两个参数 function runAsync(){ var p = new Promise(function(resolve ...
- php数组合并用加号(+)和用array_merge()的区别
结论:用加号合并数组:既考虑数字索引的键值对,也考虑字符串索引的键值对,用前边数组的值覆盖后边的键名相同的值; 用array_merge()合并数组:只考虑字符串索引的键值对,用后边数组的值覆盖掉前面 ...
- centos7没有IP地址
查看网卡 ip addr查看网卡 我截图中有ip,是因为我已经设置过了. eth0是对外的网卡,我们接下来设置这个网卡,你的网卡名字可能和我的不一样. 修改网卡 修改/etc/sysconfig/ne ...
- lr_save_string和sprintf、lr_eval_string的使用
一.lr_save_string函数 1.该函数主要是将程序中的常量或变量保存为参数: //将常量保存为参数 lr_save_string("777","page&quo ...
- Android NDK下载
http://dl.google.com/android/ndk/android-ndk-r10d-linux-x86_64.bin https://dl.google.com/android/rep ...
- Vue知识整理11:列表渲染(v-for来实现)
简单的v-for结构显示迭代数据 通过value别名 显示下面各个属性值 通过index 和key获取同类数组索引,或者不同属性的key属性名
- Python基本语法_集合set/frozenset_内建方法详解
目录 目录 前言 软件环境 可变集合Set set函数创建集合 创建空集合 集合元素的唯一性 集合推导式 set类型对象的内置方法 add增加一个元素 remove删除一个元素 pop随机删除并返回一 ...
- Nginx1.8源码包编译安装
1.下载解压Nginx,为方便管理下载包一般将下载包放在指定目录下 ,即/usr/local/src/下. wget http://nginx.org/download/nginx-1.8.0.tar ...
- 测开之路一百二十八:flask之重定向和404
a.b两个视图,分别返回a的页面和b的页面 重定向:redirect 重定向到路由:请求/a/时,重定向到/b/ 重定向到视图函数:url_for(“函数名“),访问/a/时,重定向到函数b() 主动 ...
- GitHub入门(一)GIT配置与Hexo博客搭建
首先安装配置Git环境,由于本人使用Windows操作系统所以从msysgit.github.io下载msysGit Windows版本,安装.(Mac一般自带Git) 安装的时候一般使用默认选项,其 ...