跳到正文

题解

题解:P14495 [NCPC 2025] Arithmetic Adaptation

mystic_clouds约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年11月13日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 4 分钟
题目
P14495

版本

  1. v2当前
  2. v1

思路

Part 1

思路

暴力枚举。

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

AC Code

C++
#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

思路

随机数。

确定一个随机数 xx,使 xx 和另一个随机数 yy 合法。

AC Code

C++
#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

思路

分类讨论。

分 77 种情况:

  • s=0s=0
    • a=1,b=−1a=1,b=-1
  • s=1s=1
    • a=2,b=1a=2,b=1
  • 1<s<9991<s<999
    • a=1,b=s−1a=1,b=s-1
  • s=999s=999
    • a=1,b=998a=1,b=998
  • s=−1s=-1
    • a=−2,b=1a=-2,b=1
  • −1>s>−999-1>s>-999
    • a=1,b=s−1a=1,b=s-1
  • s=−999s=-999
    • a=−1,b=−998a=-1,b=-998

AC Code

C++
#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);
	cin>>s;
	if(s==0)
	{
		cout<<"1 -1";
	}
	if(s==1)
	{
		ll x=-1,y=2;
		cout<<x<<" "<<y;
	}
	if(s>1&&s!=999)
	{
		ll x=1,y=s-1;
		cout<<x<<" "<<y;
	}
	if(s==999)
	{
		cout<<"1 998";
	}
	if(s==-1)
	{
		ll x=-2,y=1;
		cout<<x<<" "<<y;
	}
	if(s<-1&&s!=-999)
	{
		ll x=1,y=s-1;
		cout<<x<<" "<<y;
	}
	if(s==-999)
	{
		cout<<"-998 -1";
	}
	return 0;
}
原文
存档评论1 条抓取于