zoukankan      html  css  js  c++  java
  • Pairs Forming LCM

    题目:

    B - Pairs Forming LCM

    Time Limit:2000MS     Memory Limit:32768KB    

    Description

    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).

    Input

    Input starts with an integer T (≤ 200), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

    Output

    For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

    Sample Input

    15

    2

    3

    4

    6

    8

    10

    12

    15

    18

    20

    21

    24

    25

    27

    29

    Sample Output

    Case 1: 2

    Case 2: 2

    Case 3: 3

    Case 4: 5

    Case 5: 4

    Case 6: 5

    Case 7: 8

    Case 8: 5

    Case 9: 8

    Case 10: 8

    Case 11: 5

    Case 12: 11

    Case 13: 3

    Case 14: 4

    Case 15: 2给定一个数字,n,求1~n之间可以找到的最小公倍数为n的对数

    解题思路:

    素因子分解:n=p1^x1*p2^x2**************pn^xn;

    a=p1^y1*p2^y2*p3^y3*****************pn^yn;

    b=p1^c1*p2^c2*p3^c3*****************pn^cn;

    gcd(a,b)=p1 ^ min(a1,b1) * p2 ^ min(a2,b2) *..........*pn ^ min(an,bn)

    lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pn ^ max(an,bn)

    所以,当lcm(a,b)==n时,max(a1,b1)==e1,max(a2,b2)==e2,…max(ak,bk)==ek

    当ai == ei时,bi可取 [0, ei] 中的所有数  有 ei+1 种情况,bi==ei时同理。

    那么就有2(ei+1)种取法,但是当ai = bi = ei 时有重复,所以取法数为2(ei+1)-1=2*ei+1。
    除了 (n, n) 所有的情况都出现了两次  那么满足a<=b的有 (2*ei + 1)) / 2 + 1 个

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <iomanip>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>y?x:y)
    #define min(x,y) (x<y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.141592653589793238462
    #define INF 0x3f3f3f3f3f
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    const int mx=10000002;
    ll pos[664583];
    ll top=0,n,t,k;
    bool vis[mx];
    void get_prime()
    {
        for(int i=2;i<mx;i++)
        {
            if(vis[i]) continue;
            pos[top++]=i;
            for(int j=2;j*i<mx;j++)
            {
                vis[i*j]=1;
            }
        }
    }
    ll solve(ll n)
    {
        ll ans=1;
        for(int i=0;i<top && pos[i]*pos[i]<=n;i++)
        {
            if(n%pos[i]==0)
            {
                int cnt=0;
                while(n%pos[i]==0)
                {
                    n/=pos[i];
                    cnt++;
                }
                ans*=(2*cnt+1);
            }
        }
        if(n>1) ans*=(2*1+1);
        return (ans+1)/2;
    } 
    int main()
    {
        get_prime();
        scanf("%lld",&t);
        k=t;
        while(t--)
        {
            scanf("%lld",&n);
            printf("Case %lld: %lld
    ",k-t,solve(n));
        }
    }
  • 相关阅读:
    VirtualBox+Windbg 进行双机调试的方法
    (串口通信编程) 开源串口调试助手Common (Com Monitor)
    Win32SDK中(串行)通信资源概要(不断更新)
    对WDK中LIST_ENTRY的遍历
    用C语言写的一个控制台界面的通讯录管理系统
    我对Windows桌面任务栏自动隐藏功能的一点小小改进不再自动弹出(20130226更新)
    对WDK中对LIST_ENTRY的操作的相关函数的实现及简单运用
    我对CONTAINING_RECORD宏的详细解释
    基于51单片机实现模拟IIC总线时序
    uploadfy 常见问题收集
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7235774.html
Copyright © 2011-2022 走看看