题解 AT4164 【[ABC102A] Multiple of 2 and N】

· · 题解

没什么好思考的,挺良心。

首先我们知道,求 2n 都能整除的最小正整数即求出它们的最小公倍数(\operatorname{lcm})。

又因为 \operatorname{lcm(a,b)}=\gcd(a,b)\times a\times b。根据公式,输出2n\times\gcd(2,n) 即可。

代码:

#include<iostream>
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int main(){
    int n=read();
    cout<<__gcd(n,2)*n*2<<'\n';
    return 0;
}