当前位置:网站首页>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
边栏推荐
- Pyth Solana is a bridge connecting reality on the chain
- How to configure webrtc video stream format playback in the new version of easygbs?
- Connect to lab server
- 嵌入式软件开发新趋势:DevOps
- 「杂谈」如何改善数据分析工作中的三大被动局面
- ros advertise 发布数据小技巧--latch配置
- rust配置国内源
- RFFE中MIPI协议
- Swin-Transformer(2021-08)
- 【DesignMode】工厂模式 (factory pattern)
猜你喜欢
随机推荐
Large file transfer software based on UDP protocol
NBI visual platform quick start tutorial (V) introduction to editor functions and operations
开发那些事儿:Linux系统中如何安装离线版本MySQL?
MySQL download and installation tutorial
Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched
熵-条件熵-联合熵-互信息-交叉熵
Neon optimization 2: arm optimization high frequency Instruction Summary
Temperature measuring instrument based on STM32 single chip microcomputer
Practical application of "experience" crawler in work
Promise从认识到使用
新版EasyGBS如何配置WebRTC视频流格式播放?
mysql函数获取全路径
期货怎么开户安全些?现在哪些期货公司靠谱些?
Cobbler轻松上手
Go语言学习教程(十)
一文详解|Go 分布式链路追踪实现原理
sql连续登录问题
Mipi protocol in RFFE
美国服务器租用和托管服务哪个好?
Kalman滤波器--从高斯融合推导









