完全平方数

【问题描述】

  从1—N中找一些数乘起来使得答案是一个完全平方数,求这个完全平方数最大可能是多少。

【输入格式】

  第一行一个数字N。

【输出格式】

  一行一个整数代表答案对100000007取模之后的答案。

【样例输入】

  7

【样例输出】

  144

【样例解释】

  但是塔外面有东西

【数据规模与约定】

  对于20%的数据,1<=N<=100.

  对于50%的数据,1<=N<=5000.

  对于70%的数据,1<=N<=10^5.

  对于100%的数据,1<=N<=5*10^6.

【上帝已经讲的很清楚了】http://blog.csdn.net/justpenz233/article/details/53039146

#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define maxn 5000010
#define mod 100000007
using namespace std;
LL n,tot,ans=1;
LL prime[maxn],f[maxn],a[maxn];
LL init()
{
    LL x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
void prepare()
{
    for(int i=2;i<=n;i++)
    {
        if(!f[i])prime[++tot]=i;
        for(int j=1;j<=tot&&prime[j]*i<=n;j++)
        {
            f[prime[j]*i]=1;
            if(i%prime[j]==0)break;
        }
    }
}
LL pow(LL x,LL m)
{
    if(m==0)return 1;
    LL he=pow(x,m/2);
    he=(he*he)%mod;
    if(m&1)he=(he*x)%mod;
    return he;
}
int main()
{
    freopen("hao.in","r",stdin);
    freopen("hao.out","w",stdout);
    n=init();
    prepare();
    for(int i=1;i<=tot;i++)
    {
        LL ha=n;
        while(ha)
        {
            a[i]+=ha/prime[i];
            ha/=prime[i];
        }
    }
    for(int i=1;i<=tot;i++)
        ans=(ans*pow(prime[i],a[i]/2*2))%mod;
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/xiaoningmeng/p/6035292.html