Loj 1236-pairs Forming LCM

Source: Internet
Author: User
Tags time limit
1236-pairs Forming LCM
PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

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 and (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

18

/p>

[

]

3

$

Case 1:2

Case 2:2

Case: 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

/* factorization n decomposition, get results n=p1^x1+p2^x2+p^x3 two number A, a take XI pi,b (xi-1,xi-2 ... 0) A total of Xi in turn B take XI a pi,a can be taken with the number of the above B plus a, a, a and a, a xi*2+1 of the situation is a factor pi scheme number, and repeated the last multiplication is all factors of the scheme number, plus only once the N, plus 1, That is twice times the total scheme time:2015-03-17 20:26 */#include <cstdio> #include <cmath> #include <cstring> #include <
queue> #include <vector> #include <algorithm> using namespace std;
typedef long Long LL;
const int MAX=10000000+10;
BOOL vis[max+10]={1,1,0,0};
vector<int>pri;
        void Init () {for (int i=2;i<=max;i++) {if (vis[i]) continue;
        for (int j=i+i;j<=max;j+=i) {vis[j]=true;
    } if (!vis[i]) pri.push_back (i);
    }} ll solve (ll N) {ll x=n;
    LL ret=1;
            for (int i=0; pri[i]*pri[i]<=x && i<pri.size (); i++) {if (x%pri[i]==0) {LL cnt=0;
                while (x%pri[i]==0) {x/=pri[i];
            cnt++;
        } ret*= ((cnt<<1) +1); }} if (x>1) {ret+= (ret&LT;&LT;1);
} return (ret+1) >>1;
    } int main () {int T;
    LL N;
    int ncase=1;
    Init ();
    scanf ("%d", &t);
        while (t--) {scanf ("%lld", &n);
        LL ans=solve (n);
    printf ("Case%d:%lld\n", Ncase++,ans);
} 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.