题目大意:求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数。
那么枚举2r的因数d,再枚举a,判断是否有满足条件的(a,b),其中a<b,并且互质。更新答案即可。
#include<bits/stdc++.h> using namespace std; #define ll long long int res; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } bool check(ll n) { ll sq = sqrt(n); return sq * sq == n; } void solve(ll rr) { for (ll i = 1;i*i<= rr; i++) //找i*i+j*j=rr,其中i<j ,并且i*i与j*j互质 { ll t = rr - i * i; if (!check(t)) //t必须是完全平方数 continue; ll j = sqrt(rr - i * i); if (i >= j) break; if (gcd(i*i, t) == 1) res++; } } int main() { ll r; scanf("%lld", &r); res = 1; r <<= 1; for (ll d = 1;d*d <= r; d++) //枚举d { if (r% d != 0) continue; solve(r / d); //a*a+b*b=r/d if (d*d== r) break; solve(d);//a*a+b*b=d } printf("%d\n", res << 2); return 0; }
这个题目在分解约数那一块,可以用 Pollard_rho进行加速。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; #define ll long long int n,ans=1; int fpow(int a,int b,int MOD) { int s=1; while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;} return s; } bool Miller_Rabin(int n) { if(n==2)return true; for(int tim=10;tim;--tim) { int a=rand()%(n-2)+2,p=n-1; if(fpow(a,p,n)!=1)return false; while(!(p&1)) { p>>=1;int nw=fpow(a,p,n); if(1ll*nw*nw%n==1&&nw!=1&&nw!=n-1)return false; } } return true; } vector<int> fac; int Pollard_rho(int n,int c) { int i=0,k=2,x=rand()%(n-1)+1,y=x; while(233) { ++i;x=(1ll*x*x%n+c)%n; int d=__gcd((y-x+n)%n,n); if(d!=1&&d!=n)return d; if(x==y)return n; if(i==k)y=x,k<<=1; } } void Fact(int n,int c) { if(n==1)return; if(Miller_Rabin(n)){fac.push_back(n);return;} int p=n;while(p>=n)p=Pollard_rho(p,c--); Fact(p,c);Fact(n/p,c); } int main() { cin>>n;Fact(n,233);sort(fac.begin(),fac.end()); for(int i=0,l=fac.size(),pos;i<l;i=pos+1) { int cnt=1; pos=i;while(pos<l-1&&fac[i]==fac[pos+1])++pos,++cnt; if(fac[i]==2)continue; if(fac[i]%4==1)ans=ans*(cnt*2+1); } printf("%d\n",ans*4); return 0; }
进一步思考题目:bzoj 椭圆上的整点
再进一步研究
https://blog.csdn.net/caoyang1123/article/details/81434665?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.no_search_link&utm_relevant_index=14
教学视频
https://www.bilibili.com/video/av12131743/
再扩展
求圆内整点数
https://blog.csdn.net/Dutch_Habor/article/details/96368323