题解:P12408 始终

· · 题解

本题题意非常清楚,在此不赘述了。

思路分析

n = 2 时,设序列为 \{a_1,a_2\},则应使 \text{lcm}(a_1,a_2)=\text{lcm}(|a_1 - a_2|)。由于 a_1\neq a_21\leq a_i\leq10^6,经过分析发现不存在满足条件的 a_1a_2,所以输出 -1。样例也体现了这个结论。

题目背景中“以我为始,以我为终”提示答案或许与循环有关。事实也确实如此。

在较小的正整数中,12 的因数较多。可以尝试构造最小公倍数为 12 的解。经过尝试,发现 \{2,6,12,6\} 符合要求。循环输出此序列即可。

Code

#include<bits/stdc++.h>

using namespace std;

constexpr int num[]={2,6,12,6};

int main(){
    ios::sync_with_stdio(false),
    cin.tie(nullptr),cout.tie(nullptr);

    int n;cin>>n;

    if(n==2)cout<<"-1";
    else for(int i=1;i<=n;++i)cout<<num[i&3]<<' '; //i & 3 == i % 4
    return 0;
}