P2508 [HAOI2008] 圆上的整点
· 阅读需 2 分钟
参考资料
题意简述
求以下方程的整数解个数:
基础知识
费马平方和定理(Fermat's theorem on sums of two squares)表明:奇素数 能表示为两个平方数之和,当且仅当 。
若:
则 的两平方和表示数为:
其中 ,且所有 均为偶数。
解题思路
本题即求 的两平方和表示数,设:
则:
由于已经是平方,所有 的指数均为偶数,故答案为:
因此只需要分解 ,统计所有 型质因子的指数即可。
时间复杂度为 。
参考代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int r;
cin>>r;
int ans=4;
for(int i=2;i*i<=r;i++)
{
if(r%i)continue;
int k=0;
while(r%i==0){r/=i;k++;}
if(i%4==1)ans*=k*2+1;
}
if(r>1&&r%4==1)ans*=3;
cout<<ans<<'\n';
return 0;
}