Remoteland
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 1111 Accepted Submission(s): 418
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 modulo 1,000,000,007. Note 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 modulo 1,000,000,007. Note 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
Recommend
这个题是要用1~n中尽可能多的数相乘的乘积组成一个完全平方数。求出能够组成的最大的完全平方数。首先就应该想到把1~n中的所有数分解质因数,如果质因子的次数是偶数刚好凑上,如果是奇数,那么只需要除以一个质因子就行了。这样我们就有两种思路,一种是先算出n!然后除上那些次数为奇数的质因子即可。但这样会引入逆元。或者就是直接算乘法,不算除法。预先要保存1~n中所有合数的乘积。相当于n!中除去了素数。然后判断小于n的所有素数在1~n中的个数,如果个数为偶数的话,那么相当于此时保存的合数的乘积中实际上是奇数个该素数的乘积,因而要乘上该素数。第二种方法要更加快一些。
参考代码:
(方法1)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
const int mod=1000000007;
int n,s;
bool flag[10000010];
int prime[1000010];
ll res[10000010];
ll pow_mod(ll x,int n)
{
ll ans=1;
while(n)
{
if(n&1) ans=(ans*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return ans;
}
void Prime()
{
REP(i,0,10000000)
flag[i]=1;
flag[0]=flag[1]=0;
s=0;
int k=floor(sqrt(10000000)+0.5);
rep(i,2,k)
{
if(flag[i])
{
prime[s++]=i;
for(int j=2; i*j<=10000000; j++)
flag[i*j]=0;
}
}
REP(i,k,10000000)
if(flag[i])
prime[s++]=i;
}
int main()
{
Prime();
res[0]=1;
REP(i,1,10000000)
res[i]=(res[i-1]*i)%mod;
while(~scanf("%d",&n)&&n)
{
ll ans=1;
for(int i=0; i<s&&prime[i]<=n; i++)
{
int num=0,t=n;
while(t)
{
num+=t/prime[i];
t/=prime[i];
}
if(num&1)
ans=(ans*prime[i])%mod;
}
printf("%I64d\n",(res[n]*pow_mod(ans,mod-2))%mod);
}
}
(方法二)(摘自别人代码)
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define N 10000002
const int mod = 1000000007;
bool a[10000002]={1,1,0};
int p[680000],num=0;
int sum[10000002]={0,1};
void prime(){
int i,j;
for(i=2;i<3164;i++)
if(!a[i])
for(j=i*i;j<N;j+=i)
a[j]=1;
for(__int64 i=2;i<N;i++)
if(!a[i]) p[num++]=i, sum[i]=sum[i-1];
else sum[i]=sum[i-1]*i%mod; //保存i之前所有合数的积
}
int main(){
prime();
int i,j,k,temp;
__int64 n,ans;
while(scanf("%I64d",&n)&&n){
ans=sum[n];
for(i=0;p[i]*2<=n;i++){
int k=0;
for(temp=n;temp;)
k+=(temp/=p[i]);
if(!(k&1)) ans=p[i]*ans%mod;
}
printf("%I64d\n",ans);
}
}