給定正整數b,求最大的整數a,滿足a*(a+b) 爲完全平方數

http://www.cnblogs.com/snake-hand/archive/2013/06/05/3120010.html

題意:給定正整數b,求最大的整數a,滿足a*(a+b) 爲完全平方數,1 <= b <= 10^9

 

解題思路:

我們設a,b的最大公約數爲g,則a與a+b的最大公約數也爲g,因爲最大公約數有性質:gcd(a,b)=gcd(a,a+b)

這樣我們就可以進一步化簡有a*(a+b)=g^2*a1*(a1+b1),其中a1與b1就一定互素了,因爲已經約去最大公約數g了,所以得到a1與a1+b1也互素。

由於要爲完全平方數,所以可以設x^2=a1,y^2=a1+b1   進而推出:b1=y^2-x^2=(y-x)*(y+x)

現在我們令n=y+x,m=y-x  很明顯n>m,由於a1與a1+b1互素,所以有gcd(x^2,y^2)=1,進而有gcd(x,y)=1

 

到了這裏本題就可以這樣做了:

先求出b的所有約數,這樣g就等於b/b的約數,然後又分別求出b的約數的約數,假設爲arr2[j],由於n>m,我們只需要枚舉到sqrt(arr1[i])即可,然後解

出x=(n-m)/2,y=(n+m)/2,但是前提是要保證都能整除,然後再判斷gcd(x,y)=1,兩層for循環,記錄最大值即可。至於怎樣求一個數因子,前面的文章已

經給出,就是素因子分解加上dfs,由於因子一般不會很多,所以一般沒有問題。建議不要看我的代碼,很亂的。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>

using namespace std;

typedef long long LL;
const int N=1000010;
const int M=1050;

bool prime[N];
LL p[N];
LL pr1[M];
LL kk1[M];

LL pr2[M];
LL kk2[M];
LL k=0;
LL c1,r1;
LL arr1[M];
LL c2,r2;
LL arr2[M];

void isprime()
{
    LL i,j;
    memset(prime,true,sizeof(prime));
    for(i=2;i<N;i++)
    {
        if(prime[i])
        {
            p[k++]=i;
            for(j=i+i;j<N;j+=i)
            {
                prime[j]=false;
            }
        }
    }
}

void CalFactor1(LL n)
{
    LL t=n,i,a;c1=0;
    for(i=0;p[i]*p[i]<=n;i++)
    {
        a=0;
        if(n%p[i]==0)
        {
            pr1[c1]=p[i];
            while(n%p[i]==0)
            {
                a++;
                n/=p[i];
            }
            kk1[c1]=a;
            c1++;
        }
    }
    if(n>1)
    {
        pr1[c1]=n;
        kk1[c1]=1;
        c1++;
    }
}

void dfs1(LL dep, LL product)
{
    if ( dep == c1 )
    {
        arr1[r1++]=product;
        return;
    }
    for ( LL i = 0; i <= kk1[dep]; ++i )
    {
        dfs1(dep + 1, product);
        product *= pr1[dep];
    }
}

void CalFactor2(LL n)
{
    LL t=n,i,a;c2=0;
    for(i=0;p[i]*p[i]<=n;i++)
    {
        a=0;
        if(n%p[i]==0)
        {
            pr2[c2]=p[i];
            while(n%p[i]==0)
            {
                a++;
                n/=p[i];
            }
            kk2[c2]=a;
            c2++;
        }
    }
    if(n>1)
    {
        pr2[c2]=n;
        kk2[c2]=1;
        c2++;
    }
}

void dfs2(LL dep, LL product)
{
    if ( dep == c2 )
    {
        arr2[r2++]=product;
        return;
    }
    for ( LL i = 0; i <= kk2[dep]; ++i )
    {
        dfs2(dep + 1, product);
        product *= pr2[dep];
    }
}

LL gcd(LL a,LL b)
{
    return b? gcd(b,a%b):a;
}

int main()
{
    LL m,n,i,j,g,b,val,t;
    isprime();
    cin>>t;
    while(t--)
    {
        cin>>b;
        r1=r2=0;
        CalFactor1(b);
        dfs1(0,1);
        sort(arr1,arr1+r1);
        LL max=0;
        for(i=0;i<r1;i++)
        {
            r2=0;
            g=b/arr1[i];
            memset(arr2,0,sizeof(arr2));
            CalFactor2(arr1[i]);
            dfs2(0,1);
            for(j=0;j<r2;j++)
            {
                if(arr2[j]*arr2[j]<arr1[i])
                {
                    n=arr1[i]/arr2[j];
                    m=arr2[j];
                    if((n-m)%2==0&&(n+m)%2==0&&gcd((n+m)/2,(n-m)/2)==1)
                    {
                        val=g*((n-m)/2)*((n-m)/2);
                        if(val>max) max=val;
                    }
                }
            }
        }
        cout<<max<<endl;
    }
    return 0;
}


發佈了49 篇原創文章 · 獲贊 11 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章