昼夜切换动画

题解 CF1594A

Description

给你一个整数 \(n\),你需要找出两个整数 \(l,r\) 使得 \(l+(l+1)+\dots(r-1)+r=n\)

Solution

第一眼等差数列,但是 \(-10^{18}\le n \le 10^{18}\) 完全无法枚举完 \(l\),\(r\),所以我们另辟蹊径。

假设 \(n=6\)

\(l=1,r=6\) 时,可以写成

\(1+2+3+4+5+6\)

但是我们只想要加和为 \(6\),怎么办?

\(1+(-1)+2+(-2)+3+(-3)+4+(-4)+5+(-5)+6\)

整理一下

\((-5)+(-4)+(-3)+(-2)+(-1)+0+1+2+3+4+5+6\)

这不就是我们要求的吗?

显然,\(l=-n+1,r=n\)

Code

代码过于简单,没有注释。


/*
* @Author: smyslenny
* @Date:    2021.10.10
* @Title:   CF1594A
* @Main idea:构造
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>

#define int long long 

using namespace std;
int T,n;
int read()
{
	int x=0,y=1;
	char c=getchar();
	while(c<'0' || c>'9') {if(c=='-') y=0;c=getchar();}
	while(c>='0' && c<='9') { x=x*10+(c^48);c=getchar();}
	return y?x:-x;
}

signed main()
{
	T=read();
	while(T--)
	{
		n=read();
		printf("%lld %lld\n",n-1,n); 
	}
	return 0;
}


posted @ 2021-10-10 08:33  smyslenny  阅读(167)  评论(0编辑  收藏  举报