Luogu P11568 「chaynOI R1 T1」一维数组 题解 [ 橙 ] [ 构造 ] [ 数学 ] [ adhoc ]

一维数组:adhoc 数学题。

根据小学乘法的经验,不难想到 \(n\)\(1\)\(m\)\(1\) 组成的数相乘的构造方式,能使得数字是回文数。证明可以参考竖式的过程。

但是这么做是会 WA 的,原因很简单,当数字位数过大的时候,会在竖式过程中产生进位,使得结果不是回文数了。

再根据小学数学的经验,我们遇到 \(0\) 时会直接跳过这一位,到下一位继续乘。而为了防止进位的出现,也就是让数字尽可能错位相加,于是我们在数字中间尽可能地多放 \(0\),只保留头尾两个 \(1\) 即可。

时间复杂度 \(O(n)\)

#include <bits/stdc++.h>
#define fi first
#define se second
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define lc(x) (tr[x].ls)
#define rc(x) (tr[x].rs)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
using pi=pair<int,int>;
int n,m;
int main()
{
    //freopen("sample.in","r",stdin);
    //freopen("sample.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    cout<<1;
    if(n>1)
    {
        for(int i=1;i<=n-2;i++)cout<<0;
        cout<<1;
    }
    cout<<" ";
    cout<<1;
    if(m>1)
    {
        for(int i=1;i<=m-2;i++)cout<<0;
        cout<<1;
    }
    return 0;
}
posted @ 2025-06-15 19:39  KS_Fszha  阅读(10)  评论(0)    收藏  举报