Lightoj 1236-pairs Forming LCM

Source: Internet
Author: User
Tags cas


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'll find that the code actually counts the number of pairs (I, j) for which LCM (i, j) = N An D (I≤J).



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



Each case is 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
Output for sample input

2
3
4
6
8

[
] r>


+
(
)
Case 1:2
Case 2:2
Case 3:3
Case 4:5
Case 5:4 br> 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
Parse: Find the number of groups for the LCM (i,j) ==n.
I=p1^a1*p2^a2*p3^a3......ps^as;
J=p1^b1*p2^b2*p3^b3......ps^b3;
N=p1^c1*p2^c2*p3^c3......ps^cs; The
LCM (i,j) ==n is P1^max (A1,B1) P2^max (A2,B2) ...
that satisfies: Max (AI,BI) ==ci, which satisfies the number of conditions. The
takes an ordered pair of numbers first.
1:ai=ci,bi=[0,ei] There are ei+1 species
2:bi=ci, Ai=[0,ei], and Ai==ci,bi==ci, the above appeared, so there is a 2*ei+1 species minus one.
for unordered pairs, the Lcm (n,n) appears only once (EI==AI==BI) so the solution is:
((2*e1+1) * (2*e2+1) ... (2*es+1) +1)/2;
for an example: n=2^3;
when a1=3, b1=3,2,1,0;
b1=3,a1=3,2,1,0; The
considers the ordered pair time (3,3) to repeat once, so subtract one time.
When multiple items are multiplied by the same time, use the multiplication rule.



The first time I met ML, I was crazy. Look at others, with the boll type instead of int, BOOL only accounted for 1 bytes, int accounted for 4 bytes.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define LL long long

const int N = 1e7 + 5;
const int NN = 1e6;
int prime [NN], cnt = 0;
bool vis [N];
void el () // prime table
{
    memset (vis, 0, sizeof (vis));
    for (int i = 2; i <N; i ++)
    {
        if (! vis [i])
        {
            prime [cnt ++] = i;
            for (int j = i + i; j <N; j + = i)
            {
                vis [j] = 1;
            }
        }
    }
}

int main ()
{
    el ();
    int t;
    LL n, sum;
    scanf ("% d", & t);
    for (int cas = 1; cas <= t; cas ++)
    {
        sum = 1;
        scanf ("% lld", & n);
        for (int i = 0; i <cnt && prime [i] * prime [i] <= n; i ++)
        {
            if (n% prime [i] == 0) // can be divisible
            {
                int e = 0;
                while (n% prime [i] == 0) // Unique decomposition theorem
                {
                    n / = prime [i];
                    e ++;
                }
                sum * = (2 * e + 1); // The types of possible values for each prime
            }
        }
        if (n> 1)
            sum * = (2 * 1 + 1); // If it cannot be decomposed in the end, it will be proved that there is still a prime number remaining, the kind is the same as before
        printf ("Case% d:% d \ n", cas, (sum + 1) / 2); // Remove duplicate cases of (i, j), (j, i)
    }
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.