lightoj 1236 - Pairs Forming LCM 【唯一分解定理】

题目链接:lightoj 1236 - Pairs Forming LCM

题意:问你满足 lcm(i,j)=n(i<=j) (i,j) 有多少对。

思路:这东西肯定要先搞下质因子。由于 n<=1014 ,那么我们预处理质因子到 107 OK 了,最后 >=107 的质因子最多只会有一个。我们将 n 分解为若干个质因子a[i](1<=i<=k)
由唯一分解定理—— n=ni=1(a[i]cnt[i]))(cnt[i]a[i])
我们想得到 lcm(i,j)=n 就必须满足 ij/gcd(i,j) 的质因子与 n 的质因子完全相同。那我们考虑单个质因子a[i]:假设 i=a[i]x j=a[i]y
这样 ij/gcd(i,j)=a[i]x+ymin(x,y) ,则有 x+ymin(x,y)=cnt[i]
一、 x=cnt[i] ,显然 cnt[i]+yy=cnt[i] ,y值任意 0<=y<=cnt[i]
二、 0<=x<cnt[i] ,显然要满足等式, y=cnt[i]
这样对于一个因子 a[i] ,方案数为 (cnt[i]+1+cnt[i]) ,但是 (i,j) (j,i) 肯定是重复计算,结果要除 2 。又去掉了(n,n)的情况,加一即可。

AC 代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e7 + 1;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
bool vis[MAXN];
const int N = 1e6;
int prime[N], top;
void getprime() {
    top = 0;
    for(int i = 2; i < MAXN; i++) {
        if(vis[i]) continue;
        prime[top++] = i;
        for(int j = 2; j * i < MAXN; j++)
            vis[j*i] = true;
    }
}
int k, cnt[N];
void getp(LL n) {
    LL m = n; k = 0;
    for(int i = 0; i < top; i++) {
        if(m % prime[i] == 0) {
            int num = 0;
            while(m % prime[i] == 0) {
                num++;
                m /= prime[i];
            }
            cnt[k++] = num;
        }
        if(prime[i] > n) break;
    }
    if(m > 1) {
        cnt[k++] = 1;
    }
}
int main()
{
    getprime();
    //cout << top << endl;
    int t, kcase = 1; scanf("%d", &t);
    while(t--)
    {
        LL N; scanf("%lld", &N);
        getp(N);
        LL ans = 1LL;
        for(int i = 0; i < k; i++) {
            ans = ans * (cnt[i] * 2 + 1);
        }
        ans >>= 1;
        printf("Case %d: %lld\n", kcase++, ans + 1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值