题解:P14495 [NCPC 2025] Arithmetic Adaptation

· · 题解

思路

Part 1

思路

暴力枚举。

双层循环遍历两边 -999 到 999 的每个数,特判是否为零,如果找到一种解,可以直接输出。

AC Code

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
ll s;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>s;
    for(ll i=-999;i<=999;i++)
    {
        for(ll j=-999;j<=999;j++)
        {
            if(i+j==s&&i!=0&&j!=0)//特判
            {
                cout<<i<<" "<<j;
                return 0;
            }
        }
    }
    return 0;
}

Part 2

思路

随机数。

确定一个随机数 x,使 x 和另一个随机数 y 合法。

AC Code

#include<bits/stdc++.h>
#include<chrono>
#define ll long long
#define ull unsigned long long
using namespace std;
ll s,x; 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    unsigned seed=chrono::steady_clock::now().time_since_epoch().count();
    mt19937 rd(seed);
    uniform_int_distribution<long long>rds(-999,999);//限定范围
    cin>>s;
    x=rds(rd);
    while(x==0||x==s||x<-999||x>999||s-x<-999||s-x>999)
    {
        x=rds(rd);
    }
    cout<<x<<" "<<s-x;
    return 0;
}

Part 3

思路

分类讨论。

分 7 种情况: