Pairs Forming LCM 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) p a i r s ( i , j ) for which lcm(i,j)=n l c m ( i , j ) = n and (ij). ( i ≤ j ) .

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

Each case starts with a line containing an integer n(1n1014). 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,找出 (i,j) ( i , j ) 使得 lcm(i,j)=n l c m ( i , j ) = n ,其中 1ijn 1 ≤ i ≤ j ≤ n
问有多少对这样的 (i,j) ( i , j )

思路:对于n我们可以写成

n=pa11pa22pann n = p 1 a 1 p 2 a 2 ⋯ p n a n

同理,i和j也可以写成这个形式
i=pb11pb22pbnn i = p 1 b 1 p 2 b 2 ⋯ p n b n
j=pc11pc22pcnn j = p 1 c 1 p 2 c 2 … p n c n

因为 lcm(i,j)=n l c m ( i , j ) = n
所以
a1=max(b1,c1)a2=max(b2,c2)an=max(bn,cn) a 1 = m a x ( b 1 , c 1 ) a 2 = m a x ( b 2 , c 2 ) ⋯ a n = m a x ( b n , c n )

其中
biorci=ai b i o r c i = a i

bi=ai b i = a i 的时候, 0ciai 0 ≤ c i ≤ a i ai+1 a i + 1 种情况
ci=ai c i = a i 的时候, 0biai 0 ≤ b i ≤ a i ai+1 a i + 1 种情况
一共有 2(ai+1) 2 ( a i + 1 ) 种情况
但是 bi=ci=ai b i = c i = a i 的情况出现了两次,所以我们要去重,去重后结果是 2ai+1 2 a i + 1 种情况
又因为 ij i ≤ j
所以最终的结果

2ans=i=1n(2ai+1) 2 ∗ a n s = ∏ i = 1 n ( 2 a i + 1 )

ans=ni=1(2ai+1)2 a n s = ∏ i = 1 n ( 2 a i + 1 ) 2

第一次做的时候没有打素数表,导致TLE一次…


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
using namespace std;
typedef long long ll;
const int MAXN=1e7+9;
bool vis[MAXN];
int prime[1000000];
void getprime()
{
    prime[0]=0;
    for(int i=2;i<MAXN;i++)
    {
        if(!vis[i]) prime[++prime[0]]=i;
        for(int j=2;j*i<MAXN;j++)
            vis[i*j]=1;
    }
}

int main()
{
    int t;
    int kase=0;
    getprime();
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        ll ans=1;
        scanf("%lld",&n);
        for(ll i=1;(long long)prime[i]*prime[i]<=n&&i<prime[0];i++)
        {
            int cnt=0;
            if(n%prime[i]==0)
            {
                while(n%prime[i]==0)
                {
                    cnt++;
                    n/=prime[i];
                }
                ans*=2*cnt+1;
            }
        }
        if(n!=1)    ans*=2*1+1;
        printf("Case %d: %lld\n",++kase,(ans+1)/2);
    }
    return   0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值