• HDU 4196 Remoteland


    题意:给定一个n,然后让你从1-n中选出某些数乘起来,使得乘积最大,并且乘积必须是完全平方数。

    思路:将1-n种每个数都分解素因子,把他们的素因子的幂加起来,如果是偶数,就说明可以构成完全平方数,乘起来,如果是奇数,说明不能构成,减去一个就是偶数了,所以减去一个再乘起来。因为要分解1-n当中所有的素因子,然后乘起来,那么也就是分解n!的素因子,所以只要找出来他的所有的素因子的幂指数为奇数的直接除就行了。现在有个问题是不能除。要取模,更好的方法是,再算n!的时候,不乘素数,那样的话,就到最后再乘。如果是奇次幂,就不用乘,偶次幂再乘。这样的话,就需要找出1-n当中素因子是多少次幂,方法是,直接用n除以素因子,然后加起来,知道n为0。具体见代码。

    吐嘈:这个题意完全读不懂啊。。。看了题解才知道的。还有就是这种方法太奇妙了。赞赞赞!

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    typedef long long ll;
    const int maxn = 10000010;
    const ll mod = 1000000007LL;
    const int sqrtmaxn = (int)(sqrt((double)maxn));
    ll fac[maxn];
    int prime[maxn];
    int cnt;
    void init()
    {
        memset(prime, 0, sizeof(prime));
        fac[0] = fac[1] = 1;//阶乘
        cnt = 0;
        for (int i = 2; i < maxn; i++)
        {
            fac[i] = fac[i - 1];
            if (prime[i] == 0)//如果是素数,就先不乘
            {
                prime[cnt++] = i;
                if (i <= sqrtmaxn)
                    for (int j = i * i; j < maxn; j += i)
                        prime[j] = 1;
            }
            else fac[i] = (fac[i] * i) % mod;//不是素数直接乘
        }
    }
    int main()
    {
        init();
        int n;
        while (~scanf("%d", &n) && n)
        {
            ll ans = fac[n];
            for (int i = 0; i < cnt && prime[i] <= n; i++)
            {
                int tot = 0, tmp = n;//tmp统计素因子的幂次
                while (tmp) tot += (tmp /= prime[i]);
                if ((tot & 1) == 0) ans = (ans * prime[i]) % mod;
            }
            printf("%d
    ", (int)ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    小程序中父子组件间的通信与事件
    关于绝对定位那些事儿 —— 与 overflow: hidden
    关于 hover 时候闪烁的问题
    大工不巧的 前端设计 和 编程艺术
    GoF “四人组” 提出的 23 种设计模式 及 JavaScript 设计模式
    原型和继承 constructor、prototype、__proto__
    js 中的类型比较
    取模 和 取余
    音频文件播放失败 Unhandled Exception: [Object DOMException]
    C语言入门:06.基本运算
  • 原文地址:https://www.cnblogs.com/Howe-Young/p/5475563.html
Copyright © 2020-2023  润新知