当前位置:网站首页>Code shoe set - mt3111 · assignment
Code shoe set - mt3111 · assignment
2022-06-30 19:31:00 【Tisfy】
Portal
assignment
The time limit :1 second
Space restriction :64M
Title Description
The adventure group is exploring in a wonderful secret place , In front of me is the reward level of the secret place , The checkpoint mechanism is as follows : Random given M Gold coin , If you can find three that are not greater than M The positive integer ( Can be the same ), Find their least common multiple S, Then the adventure group can get S Gold coins . In order to get the most gold coins , It is unanimously decided that you, the team think tank, will be entrusted with the task , Find out S The maximum of .
Input description
Enter a positive integer n
- 1<=n<=1e6
Output description
Output the least common multiple of the three numbers in the question S The maximum of
Example 1
Input
10
Output
630
Topic analysis
The title of this problem can be summarized as : Seek from 1 ∼ n 1\sim n 1∼n The maximum value of the least common multiple of any three numbers .
This problem is actually a mathematical problem . The complexity of trying to enumerate three numbers violently is O ( n 3 ) O(n^3) O(n3), It's bound to time out .
So we need to use the mathematical properties of Coprime . The following conclusions may not prove , But it does not affect the passage of this question .
Obviously , When three numbers are coprime , The least common multiple of three numbers is the largest .
Method 1 : The mathematical formula
- n n n In an odd number of , n n n、 n − 1 n-1 n−1、 n − 2 n-2 n−2 Coprime
- n n n For even when
- n n n Can be 3 3 3 Divisible time , n − 1 n-1 n−1、 n − 2 n-2 n−2、 n − 3 n-3 n−3 Coprime
- n n n Can not be 3 3 3 Divisible time , n n n、 n − 1 n-1 n−1、 n − 3 n-3 n−3 Coprime
Some people may not be convinced , Why the above properties . Here interested students can go to search , But this article does not prove it here , Because there is another way .
Method 2 : Local violence
I really encounter such a problem , I don't know what to do with the mathematical formula of method one ?
No problem , Greed can pass .
First of all, I want to take the opportunity as big as possible , We have to get these three numbers as large as possible .
So we can n n n In the vicinity of . The time limit for this question is 1 s 1s 1s, Here 1 s 1s 1s In the time of , Conduct 1 0 6 10^6 106 The amount of calculation is not excessive !
All we need to do is [ max ( 1 , n − 100 ) , n ] [\max(1, n - 100), n] [max(1,n−100),n] Enumeration within the scope of i , j , k i,j,k i,j,k, Can be in 1 0 6 10^6 106 Get the best answer in the level of computation .
Method 3 : The formula + No brain
The second method is mindless computing , Method 2 is adopted because “ Do not know the mathematical formula of method one ”. In fact, we just need to [ max ( 1 , n − 3 ) , n ] [\max(1, n - 3), n] [max(1,n−3),n] The best answer can be obtained by enumerating three numbers within the range of .( For the time being, it is recorded as method 3 : The formula + No brain )
AC Code
Method 1 : The mathematical formula
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main() {
ll n;
cin >> n;
if (n % 2) {
cout << n * (n - 1) * (n - 2) << endl;
}
else {
if (n % 3 == 0) {
cout << (n - 1) * (n - 2) * (n - 3) << endl;
}
else {
cout << n * (n - 1) * (n - 3) << endl;
}
}
return 0;
}
Method 2 : Local violence
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = 0;
for (ll i = max(1ll, n - 100); i <= n; i++)
for (ll j = max(1ll, n - 100); j <= n; j++)
for (ll k = max(1ll, n - 100); k <= n; k++) {
/* // Error! There is a wrong method in the comments 400 400 401 Of gcd by 1,400*400*401/1/1 != 400 * 401 ll gcd = __gcd(__gcd(i, j), k); ans = max(ans, i * j * k / gcd / gcd); printf("i = %lld, j = %lld, k = %lld, __gcd(i, j, k) = %lld, GBS = %lld\n", i, j, k, gcd, i * j * k / gcd / gcd); */
ll thisAns = i * j / __gcd(i, j);
thisAns = thisAns * k / __gcd(thisAns, k);
ans = max(ans, thisAns);
}
cout << ans << endl;
return 0;
}
Method 3 : The formula + No brain
Running time is greatly reduced
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = 0;
for (ll i = max(1ll, n - 3); i <= n; i++)
for (ll j = max(1ll, n - 3); j <= n; j++)
for (ll k = max(1ll, n - 3); k <= n; k++) {
ll thisAns = i * j / __gcd(i, j);
thisAns = thisAns * k / __gcd(thisAns, k);
ans = max(ans, thisAns);
}
cout << ans << endl;
return 0;
}
Update the weekly competition solution of elite class in advance every week , Focus , Neverlost
Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125538415
边栏推荐
- Introduction to Po mode "suggestions collection"
- 一文详解|Go 分布式链路追踪实现原理
- VS 常用的快捷键指令
- 微信小程序快速入门 --项目介绍
- mysql修改数据类型_MySQL修改字段类型[通俗易懂]
- 年复一年,为什么打破数据孤岛还是企业发展的首要任务
- Connect to lab server
- 10 statistical methods commonly used for "dry goods" data analysis, with key application scenarios attached
- How JS correctly clears all child elements under an element
- 20200525-生物技术-四川师范大学自考生物技术(本科)考试计划.txt
猜你喜欢

Brief introduction of Feature Engineering in machine learning

美国服务器租用和托管服务哪个好?

不同制造工艺对PCB上的焊盘的影响和要求

Evolution of screen display technology

How to use the low code platform of the Internet of things for service management?

Kalman filter -- Derivation from Gaussian fusion

在广州的朋友,有机会可以参加下

Opencv data type code table dtype

4个技巧告诉你,如何使用SMS促进业务销售?

dtd建模
随机推荐
The folder is transferred between servers. The folder content is empty
Unity技术手册-初探性能优化
连接实验室服务器
Coding officially entered Tencent conference application market!
Word——Word在试图打开文件时遇到错误的一种解决办法
nats集群部署
German agbb VOC hazardous substances test
Promise从认识到使用
简述机器学习中的特征工程
PO模式简介「建议收藏」
教你Selenium 测试用例编写
Makefile笔记(一文学会Makefile)
MQ的选择(2022.5.9-5.15)
Construction and practice of full stack code test coverage and use case discovery system
Swin-transformer --relative positional Bias
熵-条件熵-联合熵-互信息-交叉熵
Nodejs 安装与介绍
力扣------统计包含给定前缀的字符串
MySQL function to get the full path
设计电商秒杀系统