[BZOJ1041 number theory]

Analysis of
seeking the number of points (x, y) satisfies x2 + y2 = r2

Obviously, the answer is symmetrical, the number four quadrants of the same.
And r is an integer, the four axes are the whole point.
Ans is the number of points disposed in the first quadrant, the result is 4 * ans + 4.

Now we assume that (x> 0, y> 0), then y = sqrt ((rx) (r + x))

Setting d = gcd ((r + x ), (rx))
then it is obvious (r + x) / d and (rx) / d coprime.
Set A = (rx) / d, B = (r + x) / d
is rx = Ad, r + x = Bd
bring it into the equation, then y2 = d2AB
i.e., (y / d) 2 = AB
since x ! = 0, A! = B.
Therefore A and B are the number of squares, i.e., A = a2, B = b2.
Thus a2 + b2 = (2r) / d and gcd (a, b) == 1

Since A = rx, B = r + x, so that a <b.
Therefore a2 <r / d

Then we enumerate a and d, obtained b, to determine whether the condition can be.

Note that, when d enumeration with both sides, i.e., (2r) / d and d.

#include <bits/stdc++.h>
#define sc(n) scanf("%d",&n)
#define pt(n) printf("%d\n",n)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
using namespace std;
typedef long long ll;
ll r,ans = 0;
void work(ll k)
{
for(ll a=1;a*a<k/2;a++)
{
ll now = k-a*a;
ll b = sqrt(now);
if(b*b!=now) continue;
if(__gcd(a,b)==1) ans++;
}
}
int main()
{
scanf("%lld",&r);
for(ll d=1;d*d<=2*r;d++)
{
if(2*r%d==0)
{
work(2*r/d);
if(d*d!=2*r) work(d);
}
}
printf("%lld\n",4*ans+4);
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11109110.html