完全平方数 hao

来源:互联网 发布:网络黄牛党 编辑:程序博客网 时间:2024/04/29 02:50

【问题描述】

从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。

做法:
对于n!分解质因数,将每个质因数的次数/2(奇数的会舍去一个),乘起来,最后平方。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define LL long long#define MOD 100000007#define M 5000009using namespace std;LL n,prime[M],cnt,num[M];bool np[M];LL ans=1;void su(){    np[0]=1;np[1]=1;    for(LL i=2;i<=n+10;i++)    {        if(!np[i]) prime[++cnt]=i;        for(LL j=1;j<=cnt&&1ll*i*prime[j]<=n+10;j++)        {            np[i*prime[j]]=1;            if(i%prime[j]==0) break;        }    }}LL Fast_pow(LL x,LL p){    LL y=1;    while(p)    {        if(p%2==1) y=(y*x)%MOD;        x=(x*x)%MOD;        p/=2;    }    return y;}int main(){    freopen("hao.in","r",stdin);    freopen("hao.out","w",stdout);    scanf("%lld",&n);    su();    for(LL i=1;i<=cnt;i++)    {        LL p=prime[i];//一定要开LL        while(p<=n)        {            num[prime[i]]+=n/p;            p*=prime[i];//如此分解质因数的方法在前面的题《组合数》中用过。(前提是N!)        }    }    for(LL i=1;i<=cnt;i++)    {        ans=(ans*Fast_pow(prime[i],num[prime[i]]/2))%MOD;    }    ans=(ans*ans)%MOD;    printf("%lld",ans);    return 0;}