Squares (AtCoder Regular Contest 125)(枚举+思维)

传送门

题意

给你一个正整数N,找出有多少个整数对(x,y),满足如下条件,答案对998244353取模

  • 1 <= N <= 1012
  • 1<= x,y <=N.
  • x*x-y是一个完全平方数.(此处0也是完全平方数)

思路

x2 - y = z2
x2 - z2 = y
(x - z)(x + z) = y
设 l = x - z, r = x + z
由题意知z >= 0,
又由上式可知, 1 <= l <= sqrt(n),l <= r <= n / l 。同时,r 与 l 同奇偶。
则,枚举l ,求出r的个数累加即为所求。

#include<cstdio>
#include<queue>
#include<set>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define mt(a,b) memset(a, b, sizeof a)
//#define int long long
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 2e5 + 7, M = 1e6;

int main()
{
	IOS;
	ll n; cin >> n;
	ll ans = 0;
	ll m = sqrt(n);
	for (int i = 1; i <= m; i++)//这里若为 i * i <= n,注意要定义为ll型,否则会溢出
	{
		ll l = i, r = n / i;//注意r的数据溢出,防止爆int
		if (l & 1)
		{
			if (r & 1) r++;//保证l,r异号,则奇数个数即为区间长度的一半
			ans = (ans + (r - l + 1) / 2) % 998244353;
		}
		else
		{
			if ((r & 1) == 0) r++;//同上
			ans = (ans + (r - l + 1) / 2) % 998244353;
		}
	}

	cout << ans << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

to cling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值