zoukankan      html  css  js  c++  java
  • LightOJ

    链接:

    https://vjudge.net/problem/LightOJ-1236

    题意:

    Find the result of the following code:

    long long pairsFormLCM( int n ) {
    long long res = 0;
    for( int i = 1; i <= n; i++ )
    for( int j = i; j <= n; j++ )
    if( lcm(i, j) == n ) res++; // lcm means least common multiple
    return res;
    }

    A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

    思路:

    考虑lcm(a, b)= n,有(a = p_1^{k1}*p_2^{k2}*p_3^{k3}, b = p_1^{t1}*p_2^{t2}, n = p_1^{e1}*p_2^{e2}).
    如果想lcm(a, b) = n,得保证对于每个p,max(ki, ti) = ei.保证每个素数满足n的指数。
    这样每个p有(2*ei+1)种取值,减去相同。
    则得到了无序对(a, b)
    有序对则加1除2,因为相同的只计算了一次。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    #include<map>
    
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 1e7+10;
    const int MOD = 1e9+7;
    
    bool IsPrime[MAXN];
    int Prime[1000010];
    int tot;
    LL n;
    
    void Init()
    {
        tot = 0;
        memset(IsPrime, 0, sizeof(IsPrime));
        IsPrime[1] = 1;
        for (int i = 2;i < MAXN;i++)
        {
            if (IsPrime[i] == 0)
                Prime[++tot] = i;
            for (int j = 1;j <= tot && i*Prime[j] < MAXN;j++)
            {
                IsPrime[i*Prime[j]] = 1;
                if (i%Prime[j] == 0)
                    break;
            }
        }
    }
    
    int main()
    {
        Init();
        int t, cnt = 0;
        scanf("%d", &t);
        while(t--)
        {
            printf("Case %d:", ++cnt);
            scanf("%lld", &n);
            LL x = n;
            LL sum = 1;
            for (int i = 1;i <= tot && Prime[i] <= x;i++)
            {
                if (x%Prime[i] == 0)
                {
                    int cnt = 0;
                    while(x%Prime[i] == 0)
                    {
                        cnt++;
                        x /= Prime[i];
                    }
                    sum *= (2LL*cnt+1);
                }
            }
            if (x>1)
                sum *= 3LL;
            sum = (sum+1)/2;
            printf(" %lld
    ", sum); 
        }
        
        return 0;
    }
    
  • 相关阅读:
    php的rabbitmq扩展(未测试)
    rabbitmq安装
    windows操作系统安装jdk以及配置环境变量
    chrome设置user-agent
    pycharm中快捷键ctrl+c,ctrl+v,ctrl+f失效问题解决
    chrome设置user agent
    Pycharm鼠标滚动控制字体大小
    mouse事件实现可拖拽的div
    DOM事件委托
    !heap 手动查询内存泄漏
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11846361.html
Copyright © 2011-2022 走看看