Hướng dẫn giải của Phương trình Pythagore nghiệm nguyên


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

const fi='';
      fo='';
      maxk=44721;
var r,re:longint;
    a:array[2..maxk] of byte;
    b,b1:array[0..5000] of longint;
    c:array[0..5000] of longint;

procedure rf;
begin
     assign(input,fi);
     reset(input);
     read(r);
     if r<0 then r:=-r;
     close(input);
end;

procedure prime;
var i,j,k:longint;
begin
     fillchar(a,sizeof(a),0);
     fillchar(b,sizeof(b),0);
     fillchar(b1,sizeof(b1),0);
     k:=trunc(sqrt(maxk));
     for i:=2 to k do
         if a[i]=0 then
         begin
              j:=i*i;
              while j<=maxk do
              begin
                   a[j]:=1;
                   j:=j+i;
              end;
         end;
     for i:=3 to maxk do
         if (a[i]=0) then
         begin
              if i mod 4 =1 then
              begin
                   inc(b[0]);
                   b[b[0]]:=i;
              end;
              if i mod 4 =3 then
              begin
                   inc(b1[0]);
                   b1[b1[0]]:=i;
              end;
         end;
end;

procedure init;
var i:longint;
begin
     while r mod 2 = 0 do r:=r div 2;
     for i:=1 to b1[0] do
         while r mod b1[i] = 0 do
               r:=r div b1[i];
end;

procedure pr;
var i,j:longint;
begin
     re:=0;
     if (r=0) then exit;
     prime;
     init;
     fillchar(c,sizeof(c),0);
     for i:=1 to b[0] do
     begin
          if r mod b[i] = 0 then
          begin
               inc(c[0]);
               c[c[0]]:=1;
               r:=r div b[i];
               while r mod b[i]=0 do
               begin
                    inc(c[c[0]]);
                    r:=r div b[i];
               end;
          end;
     end;
     if r<>1 then
     begin
          inc(c[0]);
          c[c[0]]:=1;
     end;
     j:=1;
     for i:=1 to c[0] do
         j:=j*(2*c[i]+1);
     j:=(j-1) div 2;
     re:=j;
end;

procedure wf;
begin
     assign(output,fo);
     rewrite(output);
     if r=0 then write(1) else write(re*8+4);
     close(output);
end;

begin
     rf;
     pr;
     wf;
end.

Code mẫu của RR

//Written by Nguyen Thanh Trung
{$R+,Q+}
{$Mode objFPC}
uses math;
const
  FINP='';
  FOUT='';
var
  n:longint;
  f1,f2:text;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1); close(f2);
end;
function gcd(a,b:longint):longint; inline;
begin
  if (a=0) or (b=0) then exit(a+b)
  else if a<b then exit(gcd(b,a))
  else exit(gcd(b,a mod b))
end;
function count(u:longint):longint; inline;
var
  sum,i,j:longint;
begin
  sum:=0;
  for i:=1 to u-1 do
    begin
      j:=trunc(sqrt(u-i*i));
      if j<=i then break;
      if (j*j+i*i=u) and (gcd(i,j)=1) and (gcd(j*j-i*i,j*i*2)=1) then
        sum+=8;
    end;
  exit(sum);
end;
procedure solve;
var
  i,x,kq:longint;
begin
  kq:=4;
  for i:=2 to n do
  begin
    if n div i<i then break;
    if n mod i=0 then
      begin
        x:=n div i;
        if x<i then break;
        kq+=count(i);
        if x<>i then kq:=kq+count(x);
      end;
  end;
  kq+=count(n);
  writeln(f2,kq);
end;
begin
  openF;
  read(f1,n); if n<0 then n:=-n;
  solve;
  closeF;
end.

Code mẫu của hieult

#include <stdio.h>
//#include <conio.h>
#include <math.h>
long UCLN(long n,long m)
{
while(1)
  {
  if(n==1||m==1)
    return 1;
  else if(n==0||m==0)
    return 0;
  else if(n>m)
    n=n-m*(n/m);
  else m=m-n*(m/n);
  }
}        
long f(long N)
{
long T=0,a;     
for(long i=1;i<sqrt(N);i++)
  {
  a=long(sqrt(N-i*i));
  if((a*a+i*i==N)||((a+1)*(a+1)+i*i==N))
    {
    //printf("%ld %ld ",N,i);
    if(UCLN(N,i)==1&&i*2!=N&&(a-i)%2!=0)
      T++;      
    }
  }
return T;
}        
main()
{
long N,KQ=0;
scanf("%ld",&N);
for(long i=1;i<=sqrt(N);i++)
  {
  if(i*i==N)
    KQ+=f(i);
  else if(N%i==0)
    KQ=KQ+f(i)+f(N/i);
  }
printf("%ld",4*(1+KQ));
//getch();
}

Code mẫu của khuc_tuan

isPrime = [True for x in range(45000)]
isPrime[0] = isPrime[1] = False
for i in range(2,45000):
    if isPrime[i]:
        for j in range(i+i,45000,i): isPrime[j]=False
n = input()
res = 4
for i in range(2,45000):
    if isPrime[i]:
        t=0
        while n%i==0:
            n/=i
            t=t+1
        if i%4==1 and t!=0: res *= t*2+1
    if i*i>n: break
if n!=1 and n%4==1: res *=3
print res

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.