【ZR #539. 【19普转提 4】和】题解
题目链接
题目
给定一个数字 ,请问有哪些区间 使得 。
请按 从小到大的顺序输出所有区间。
思路
根据题意我们可以列出方程:
也就是:
于是我们可以枚举 的因数 ,令 ,判断是否有两数和为 ,差为 即可。(这里假设 )
枚举因数是 ,判断 ,固总时间复杂度 。
总结
这题是一条非常经典的推式子+分解因数,只要把式子列出来发现规律就行了。
这种题在比赛是遇到一定不要慌,慢慢写出来,注意细节即可。
Code
// Problem: D. 【19普转提 4】简单MST
// Contest: UOJ - 丽泽普及2022交流赛day8
// URL: http://zhengruioi.com/contest/1074/problem/542
// Memory Limit: 512 MB
// Time Limit: 5000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define N
//#define M
//#define mo
int n, m, i, j, k;
int x, y, l, r;
signed main()
{
// freopen("tiaoshi.in","r",stdin);
// freopen("tiaoshi.out","w",stdout);
n=read()*2;
for(x=sqrt(n); x>=1; --x)
if(n%x==0)
{
y=n/x;
l=(y-x+1)/2;
r=l+x-1;
if(l+r==y&&l!=r)
printf("%lld %lld\n", l, r);
}
return 0;
}
/*
(l+r)*(r-l+1)/2=n
(l+r)*(r-l+1)=n*2
l+r-(r-l+1)=l+r-r+l-1=2l-1
l=(x-y+1)/2
r=l+y
*/
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/15778449.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!