LightOJ 1236 - Pairs Forming LCM(素因子分解)

题意 给你一个数n 求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数

容易知道 n 是a, b的所有素因子取在a, b中较大指数的积

先将n分解为素数指数积的形式 n = π(pi^ei) 那么对于每个素因子pi pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi

先不考虑a, b的大小 对于每个素因子pi

1. 在a中的指数 ai == ei 那么 pi 在 b 中的指数可取 [0, ei] 中的所有数 有 ei + 1 种情况

2. 在a中的指数 ai < ei 即 ai 在 [0, ei) 中 那么 pi 在 b 中的指数只能取 ei 有 ei 种情况

那么对与每个素因子都有 2*ei + 1种情况 也就是满足条件的 (a, b) 有π(2*ei + 1)个 考虑大小时除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有(π(2*ei + 1)) / 2 + 1

#include <iostream>
#include <stdio.h>
typedef long long LL;
using namespace std;;
const int N=1e7+5;
int p[N/10],k,i,j;
bool flag[N];//默认是false
void sushu()
{
    k=0;
    for(i=2;i<N;i++)
    {
        if(!flag[i])
        {
            p[k++]=i;
            for(j=i;j<N;j=j+i) //j=i 不是j=2
            flag[j]=true;
        }
    }
}
int main()
{
    sushu();
    int t,cas=0;
    LL n,ans,c;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ans=1;
        for(i=0;i<k;i++)
        {
            if(LL(p[i])*p[i]>n)
            break;
            c=0;
            while(n%p[i]==0)
            {
                n/=p[i];
                c++;
            }
            if(c) ans*=2*c+1;
        }
        if(n>1) ans*=3;
        printf("Case %d: %lld\n",++cas,ans/2+1); //大写的C
    }
    return 0;
}
时间: 2024-12-14 02:50:08

LightOJ 1236 - Pairs Forming LCM(素因子分解)的相关文章

LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairs

LightOJ 1236 - Pairs Forming LCM (LCM&#183;唯一分解)

题意  给你一个数n  求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数 容易知道 n 是a, b的所有素因子取在a, b中较大指数的积 先将n分解为素数指数积的形式  n = π(pi^ei)    那么对于每个素因子pi  pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi 先不考虑a, b的大小  对于每个素因子pi 1. 在a中的指数 ai == ei   那么 pi 在 b 中的指数可取 [0, ei] 中的所有数  有 ei + 1

1236 - Pairs Forming LCM -- LightOj1236 (LCM) 给你一个数n,让你求1到n之间的数(a,b &amp;&amp; a&lt;=b)两个数的最小公倍数等于n有多少对这样的ab.

题意:http://www.lightoj.com/volume_showproblem.php?problem=1236 解答:http://www.cnblogs.com/linliu/p/5549544.html 素数太大用bool #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map

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 )

H - Pairs Forming LCM(唯一分解定理)

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 r

light_oj 1236 求最小公倍数( lcm(a,b) )等于n的数对 素因数分解

light_oj 1236 求最小公倍数( lcm(a,b) )等于n的数对  素因数分解 H - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairsFormL

lightoj 1236 正整数唯一分解定理

A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;   

HDU44979 GCD and LCM (素因子分解+计数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意: 求有多少种(x,y,z)使得最小公倍数为l,最大公约数为g 分析: 我们将l,g进行素因子分解: 很明显当g有l没有的素因子 和g的某一个因子的次数大于l的这个因子的次数的时候答案为0: 然后是有答案的情况下,我们设g中某一个因子数的次数为num1,l中这个因子的次数为num2; 那么在决定x,y,z在这个因子上的次数时我们要这样考虑,至少有一个为num1,至少有一个为 num2,然后

kuangbin 带你飞 数学基础

模版整理: 晒素数 void init() { cas = 0; for (int i = 0 ; i < MAXD ; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2 ; i < MAXD ; i++) { if (is_prime[i]) { prime[cas++] = i; for (int j = i + i ; j < MAXD ; j += i) is_prime[j] =