连续自然数和

本文介绍了一种通过枚举优化解决特定数学问题的方法。针对给定数值,算法通过逆向推导找到符合条件的整数区间,巧妙地利用了数学特性进行加速。文章提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://wikioi.com/problem/1312/

题目看起来比较水,但是,数学的思维还是很重要的

直接枚举TLE;

因此,我们可以枚举长度;

A、当长度为奇数的时候,中间数必定为整数

B、当长度为偶数的时候,中间数为整数 / 2(以数学角度就是存在两个中间数)

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<iomanip>

using namespace std;

int main()
{
	int n ;
	scanf( "%d" , &n ) ; 
	for( int i = sqrt( 2 * n ) ; i >= 2 ; i -- )
	{
		if( n % i == 0 && i % 2 == 1 )
		{
			int temp = n / i ;
			cout << temp - i / 2  << " " << temp + i / 2 << endl ; 
		}
		else if( 2 * n % i == 0 && ( 2 * n / i % 2 == 1 ) )
		{
			int temp = n / i ;
			cout << temp - i / 2 + 1  << " " << temp + i / 2 << endl ; 
		}
	} 
	return 0 ;
}


 

 

转载于:https://www.cnblogs.com/pangblog/p/3249283.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值