Remoteland

9 篇文章 0 订阅
3 篇文章 0 订阅

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.
 

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值