Remoteland(标题为转载链接)
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
#include <iostream>
#include <cstdio>
using namespace std;
int const maxSize=10000000;
long long ans[10000005];
int isPrime[10000005];
int primes[maxSize],l;
void init()//用于求出在范围之内的所有的素数,顺便将范围内的所有的合数相乘得到ans[i],ans[i]表示不小于i的所有的合数的乘积,并求模
{
ans[0] = ans[1] = 1;
l = 0;
for (int i=2; i<=maxSize; i++)
{
ans[i] = ans[i-1];//继承i-1的ans
if (!isPrime[i])
{
primes[l++] = i;//记录(范围内)所有的素数
if (i < 3163)
for (int j=i*i; j<=maxSize; j+=i)//标记合数,合数为true
isPrime[j] = true;
}
else
ans[i] = (ans[i] * i) % 1000000007;//求ans[i]
}
}
int main()
{
init();
int n;
while(~scanf("%d", &n)&& n)
{
long long num = ans[n];
for (int i=0; i<l && primes[i] <= n/2; ++i)//n/2是因为n以内的素数范围不会大于n/2,更准确地说是n的算术平方根
{
int cnt = 0;
int tn = n;
while(tn >= primes[i])//这里很巧妙的求出了某一个素数在基本算数定理中的幂
{
tn /= primes[i];
cnt += tn;
}
if (cnt % 2 == 0)//由代码上面的解释而来
num = (num * primes[i]) % 1000000007;
}
printf("%lld\n", num);
}
return 0;
}