【数学】【P5077】 Tweetuzki 爱等差数列

Description

Tweetuzki 特别喜欢等差数列。尤其是公差为 1 且全为正整数的等差数列。

显然,对于每一个数 s,都能找到一个对应的公差为 1 且全为正整数的等差数列各项之和为 s。这时,Tweetuzki 想知道,满足这样条件的等差数列,最小的首项是多少。

由于 Tweetuzki 的数学非常差,尤其是因式分解,所以请你告诉他结果。

Input

输入仅包含一行一个整数 s

Output

输出一行两个用空格隔开的整数代表首项和末项

Hint

对于 10% 的数据,1  s  106
对于 100% 的数据,1  s  1012

Solution

考虑前 10% 的点,暴力枚举首项,枚举完首项就可以 O(1) 判断是否合法了。期望得分 10 pts

考虑剩下的部分:

一个等差数列的长度只有为奇数和偶数两种可能,下面对这两种可能分类讨论:

对于长度为奇数的情况,设这个等差数列共有 2x + 1 项,其中中项(最中间)为 ak

因为 aki + ak+i = 2 × ak,所以  ai = (2x + 1) ak = s

同理,对于长度为偶数的情况,设共有 2x 项,其中中间两项为 ak , ak+1,因为公差为1,所以即为 ak , ak + 1

于是  ai = x(ak+ak + 1) = x (2ak + 1) = s

由此我们得到了两种情况的关系式

s = {(2x + 1) akx (2ak + 1)

其中第一种情况长度为奇数,第二种情况长度为偶数。

注意到这两个式子都是 s 的因数分解,于是考虑直接枚举 x 的因数。

在第一种情况下,因为 ak 是它的因数,我们考虑枚举 ak,只要枚举到第一个合法的 ak 即可停止,

第二种情况下,因为 x 是它的因数,我们考虑枚举 x,计算所有合法的 ak

另外记得特判一个数是由自己做等差数列的情况

Code

#include <cmath>
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#define putchar(o) \
puts("I am a cheater!")
#endif
#define rg register
#define ci const int
#define cl const long long

typedef long long int ll;

namespace IPT {
	const int L = 1000000;
	char buf[L], *front=buf, *end=buf;
	char GetChar() {
		if (front == end) {
			end = buf + fread(front = buf, 1, L, stdin);
			if (front == end) return -1;
		}
		return *(front++);
	}
}

template <typename T>
inline void qr(T &x) {
	rg char ch = IPT::GetChar(), lst = ' ';
	while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
	while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
	if (lst == '-') x = -x;
}

template <typename T>
inline void ReadDb(T &x) {
	rg char ch = IPT::GetChar(), lst = ' ';
	while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
	while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
	if (ch == '.') {
		ch = IPT::GetChar();
		double base = 1;
		while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
	}
	if (lst == '-') x = -x;
}

namespace OPT {
	char buf[120];
}

template <typename T>
inline void qw(T x, const char aft, const bool pt) {
	if (x < 0) {x = -x, putchar('-');}
	rg int top=0;
	do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
	while (top) putchar(OPT::buf[top--]);
	if (pt) putchar(aft);
}

ll s, ans = 1000000000000ll, ss;

int main() {
	freopen("1.in", "r", stdin);
	qr(s);
	for (rg ll i = 2; (i * i) <= s; ++i) if(!(s % i)) {		//枚举中项 
		ll k = s / i;
		if (k & 1) {
			ll x = k >> 1;
			if ((i - x) > 0) {
				ans = i - x;
				ss = k;
				break;
			}
		}
	}
	for (rg ll i = 1;  (i * i) <= s; ++i) if (!(s % i)) {	//枚举x 
		ll k = s / i;
		if (k & 1) {
			ll a = k >> 1;
			if ((a - i) > 0) {
				if (ans > a - i) ans = a - i + 1, ss = i << 1;
			}
		}
	}
	if (ans == 1000000000000ll) printf("%lld %lld\n", s, s);
	else {
		qw(ans, ' ', true);
		qw(ans + ss - 1, '\n', true);
	}
	return 0;
}
posted @   一扶苏一  阅读(348)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示