跳到正文

题解

题解:P11701 [ROIR 2025] 平方差

wzb13958817049约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年5月2日
更新
2026年5月2日
篇幅
约 1 分钟
题目
P11701

版本

  1. v1当前

思路

由题目标题的提示不难想到平方差公式 x2−y2=(x+y)×(x−y)x^2 - y^2 = ( x + y ) \times ( x - y ),可知枚举的 x+yx + y 和 x−yx - y 都是 dd 的因数,所以我们可以枚举 dd 的因数从而达到 O(d)O(\sqrt{d})。

代码

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll l,r,d,ans;
int main(){
	ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
	cin>>d>>l>>r;
	for(int i=1;i*i<=d;i++){
		if(d%i==0){
			//x+y=i x-y=d/i;
			if((i+d/i)%2==0){
				ll x=(i+d/i)/2;
				ll y=x-i;
				if(y*y<x*x && l<=y*y && x*x<=r) ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}
原文