P2508 [HAOI2008]圆上的整点

P2508 [HAOI2008]圆上的整点

题意

求一个给定的同 \(\left(x^{2}+y^{2}=r^{2}\right),\) 在圆周上有多少个点的坐标是整数。

设正整数\(n=2^{a_0}p_{1}^{a_1}\dots p_{s}^{a_s}q_{1}^{b_1}\dots q_{t}^{b_t}\),其中\(p_1,\dots ,p_s,q_1,\dots ,q_t\)是不同的质数,\(p_i\equiv 1\pmod{4}(1\leq i \leq s)\)\(q_j\equiv 3\pmod{4}(1\leq j \leq t)\),而\(a_0,\dots ,a_s,b_1,\dots ,b_t\)是非负整数,则当\(b_1,\dots ,b_t\)至少有一个为奇数时,\(f(n)=0\)。而当\(b_1,\dots ,b_t\)都是偶数时,\(f(n)=4(a_1+1)(a_2+1)\dots (a_s+1)\),函数\(f(n)\)表示方程\(x^2+y^2=n\)的整数解个数。(非负整数解个数是f(n)/4)

上述定理中 \(f(n)\) 函数表示方程 \(x^{2}+y^{2}=n\) 的正整数解个数.于是我们可以运用此定理 ,将题目中的 \(r\) 分解质因数 ,由于是 \(x^{2}+y^{2}=r^{2},\) 所以 \(b_{1} \ldots b_{t}\) 肯定都为偶数,直接将r的因数乘2后带入上述式子即可。

#include <bits/stdc++.h>
using namespace std;
map<int,int>cnt;
void get(int n){
    int temp=n;
    int srt=sqrt(n);
    int num=0;
    for(int i=2;i<=srt+1;i++){
        while(temp%i==0){
            temp/=i;
            cnt[i]++;
        }
    }
    if(temp>1){
        cnt[temp]++;
    }
}
int main () {
    int r;
    scanf("%d",&r);
    get(r);
    int ans=4;
    for(auto x:cnt){
        int p=x.first,a=x.second;
        a*=2;
        if(p%4==1){
            ans=ans*(a+1);
        }
    }
    printf("%d\n",ans);
}

引用: https://www.luogu.com.cn/blog/Guess00/solution-P2508

posted @ 2021-03-22 15:12  UCPRER  阅读(75)  评论(0编辑  收藏  举报