转自:
http://blog.youkuaiyun.com/acm_cxlove/article/details/7422265
Remoteland
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 399 Accepted Submission(s): 156
Problem Description
In the Republic of Remoteland, the people celebrate their independence day every year. However, as it was a long long time ago, nobody can remember when it was exactly. The only thing people can remember is that today, the number of days elapsed since their independence (D) is a perfect square, and moreover it is the largest possible such number one can form as a product of distinct numbers less than or equal to n.
As the years in Remoteland have 1,000,000,007 days, their citizens just need D modulohttp://blog.youkuaiyun.com/acm_cxlove/article/details/7422265Note that they are interested in the largest D, not in the largest D modulo 1,000,000,007.
As the years in Remoteland have 1,000,000,007 days, their citizens just need D modulohttp://blog.youkuaiyun.com/acm_cxlove/article/details/7422265Note that they are interested in the largest D, not in the largest D modulo 1,000,000,007.
Input
Every test case is described by a single line with an integer n, (1<=n<=10,000, 000). The input ends with a line containing 0.
Output
For each test case, output the number of days ago the Republic became independent, modulo 1,000,000,007, one per line.
Sample Input
4 9348095 6297540 0
Sample Output
4 177582252 644064736
Source
SWERC 2011
其实求解逆原的是重点,任何数 A ^ A^*(mod-2) == 1 % (mod) mod=1,000,000,007, A 的摸上mod 逆原就是A^*(mod-2) ,任何数 除 A 相当于 * A^*(mod-2)
题目:用不大于n内的所有数去组成一个尽可能大的完全平方数。
http://acm.hdu.edu.cn/showproblem.php?pid=4196
完全平方数,显然是所有的素因子的个数都是偶数,便取N!的所有素因子的个数,便 有了初步想法,算出N!的什么素因子个数,奇数的就舍去一个,偶数的全要,然后再全部乘起来,可是因为规模很大,即使快速幂乘也是会超时。
于是考虑把N!除掉那些奇数个因子的乘积,便是求a=c/b,由于是取模的,直接除必然不行。
考虑c%mod=(a%mod)*(b%mod);令A=a%mod; B=b%mod;C=c%mod;
则A=a%mod=(a*1)%mod=a%mod*1%mod=(a%mod)*(b^(mod-1))%mod --------因为(b^(mod-1))%mod=1,费马小定理。
=(a*b)%mod*(b^(mod-2))%mod=(c%mod)*(b%mod)^(mod-2)%mod=C*B^(mod-2)
利用这个,就可以求出c/b的结果。
则题目求解的步骤便是:
1、打素数表
2、计算N!中含有素因子个数,如果为奇数就保留这个素因子的乘积,以便最后相除
3、求出除法结果
其实求解逆原的是重点,任何数 A ^ A^*(mod-2) == 1 % (mod) mod=1,000,000,007, A 的摸上mod 逆原就是A^*(mod-2) ,任何数 除 A 相当于 * A^*(mod-2)
#include<iostream>
#include<cstdio>
#include<cmath>
#define N 10000000
#define MOD 1000000007
#define LL long long
using namespace std;
bool flag[N+5]={0};
int prime[1000000],cnt=0;
void Prime(){
for(int i=2;i<sqrt(N+1.0);i++){
if(flag[i])
continue;
for(int j=2;j*i<=N;j++)
flag[i*j]=true;
}
for(int i=2;i<=N;i++)
if(!flag[i])
prime[cnt++]=i;
}
int getsum(int n,int p){
int sum=0;
while(n){
sum+=n/p;
n/=p;
}
return sum;
}
LL PowMod(LL a, LL b){
LL ans=1;
while(b){
if(b&1)
ans=(ans*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return ans;
}
LL fac[N+5];
int a;
int main(){
int n;
fac[1]=1;
for(int i=2;i<=N;i++)
fac[i]=(fac[i-1]*i)%MOD;
Prime();
while(scanf("%d",&n)!=EOF&&n){
LL ret=1;
for(int i=0;i<cnt&&prime[i]<=n;i++){
a=getsum(n,prime[i]);
if(a&1)
ret=(ret*prime[i])%MOD;
}
printf("%I64d\n",(fac[n]*PowMod(ret,MOD-2))%MOD);
}
return 0;
}