Squares(arc125)
作者:互联网
Squares
https://atcoder.jp/contests/arc125/tasks/arc125_b
题意:
给定一个n,找一对x,y满足以下条件:
1≤x,y≤N;
x^2-y是个完全平方数(可以是0);
找出有多少对x,y满足条件;
N<=1e12;
思路:
假设最后的完全平方数为z,则x2-y=z2;
x2-z2=y;(x+z)*(x-z)=y;
设p=(x+z),q=(x-z);
则p>=q,pq=y;
1<=x=(p+q)/2<=N;
1<=y=pq<=N;
所以:pq<=N;(p+q)/2<=N;则q<=根号n;并且p和q的奇偶性是一样的;
所以去枚举q即可;
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=998244353;
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin>>n;
int k=sqrt(n);
int ans=0;
for(int q=1;q<=k;q++){//枚举q
int p=n/q;//计算p的最大值;
ans=(ans+(p-q)/2+1)%mod;//因为p和q奇偶性一样,通过(p-q)/2+1计算可得;
}
cout<<ans<<endl;
return 0;
}
标签:arc125,pq,int,long,tie,Squares 来源: https://www.cnblogs.com/CurryBP/p/15177191.html