题解:P2508 [HAOI2008] 圆上的整点
lailai0916 · · 题解
参考资料
- Fermat's theorem on sums of two squares - Wikipedia
- 【官方双语】隐藏在素数规律中的π - bilibili
题意简述
求以下方程的整数解个数:
基础知识
费马平方和定理(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;
}