题解:P14495 [NCPC 2025] Arithmetic Adaptation

· · 题解

思路

先想想自己为什么会找到这道题。

十分简单,找到一个数 ab ,随机找满足b != 0 && a != 0 && b <= 999 && b >= -999 && a <= 999 && a <= -999 && a + b == n

为了效率,只用

while(n-t==0||t==0||n-t>999||n-t<-999||t>999||t<-999)

判断即可。其中 t 是一个随机数,那么 a=t , b=n-t ,找到满足以上条件的 ab 即可。

代码如下

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;cin>>n;
    srand(time(NULL));
    int t=rand()%999;
    while(n-t==0||t==0||n-t>999||n-t<-999||t>999||t<-999)
          t=(rand()+t)%999;//rand()+t 以防超时
    cout<<n-t<<" "<<t;
}