【代码超详解】LightOJ 1236 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) 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 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

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

二、算法分析说明

在这里插入图片描述

三、AC 代码

这里采用 https://blog.youkuaiyun.com/qq_40924940/article/details/86533392 的 AC 代码,因为我本人写的代码莫名其妙超时。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7+5;
#define ll long long int
bool is_prime[maxn];
ll prime[maxn/10],tot;
void judge()
{
    memset(is_prime,true,sizeof is_prime);
    tot = 0;
    is_prime[0] = is_prime[1] = false;
    for(ll i=2;i<maxn;i++)
    {
        if(is_prime[i])
        {
            prime[tot++] = i;
            for(ll j=i+i;j<maxn;j+=i)
            {
                is_prime[j] = false;
            }
        }
    }
}
ll slove(ll n)
{
    ll res = 1;
    for(int i=0;i<tot;i++)
    {
        if(n % prime[i] != 0)continue;
        if(prime[i] * prime[i] > n)continue;
        ll k = 0;
        while(n % prime[i] == 0)
        {
            n /= prime[i];
            k ++;
        }
        res *= (k*2+1);
    }
    if(n > 1)
        res *= 3;
    return res;
}
int main()
{
    int t,cas = 1;
    scanf("%d",&t);
    judge();
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        ll num = slove(n);
        printf("Case %d: %lld\n",cas++,(num+1)/2);
    }
    return 0;
}

本人的代码(TLE,原因不明):

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<bitset>
#pragma warning(disable:4996)
using namespace std;
unsigned long long prime[664579], _PrimeTy, MaxPrime, * prime_end = prime; bitset<10000001> notprime;
inline void gen_prime() {
	decltype(_PrimeTy) n = notprime.size() - 1, m = n / 2, L; notprime[0] = notprime[1] = true;
	for (decltype(_PrimeTy) i = 2; i <= m; ++i) {
		if (!notprime[i]) { *prime_end = i, ++prime_end; }
		L = n / i;
		for (auto j = prime; j != prime_end && *j <= L; ++j) {
			notprime[i * *j] = true; if (i % *j == 0)break;
		}
	}
	for (decltype(_PrimeTy) i = m + 1; i <= n; ++i)
		if (!notprime[i]) { *prime_end = i, ++prime_end; }
	MaxPrime = *(prime_end - 1);
}
template<class _Ty, class _Rty = unsigned> inline _Rty f(_Ty X) {//打表的最大质数的平方不能超过X的类型_Ty能够存储的最大值,否则计算出的分解上限L会溢出,导致分解成功与否判断出错。
	static _Ty L = MaxPrime * MaxPrime; _Rty m, n = 1; _Ty t = min((_Ty)sqrt(X), MaxPrime);
	for (auto d = prime; *d <= t; ++d) {
		m = 0;
		while (X % *d == 0) { X /= *d, ++m; }
		n *= 2 * m + 1, t = min((_Ty)sqrt(X), MaxPrime);
		if (X == 1)return n;
	}
	if (X < L)return 3 * n;
	return -1;
}
unsigned t, c; unsigned long long n, a, b;
int main() {
	gen_prime();
	scanf("%u", &t);
	for (unsigned h = 1; h <= t; ++h) {
		scanf("%llu", &n);
		printf("Case %u: %u\n", h, (f(n) + 1) / 2);
	}
	return 0;
}
代码转载自:https://pan.quark.cn/s/7f503284aed9 Hibernate的核心组件总数达到五个,具体包括:Session、SessionFactory、Transaction、Query以及Configuration。 这五个核心组件在各类开发项目中都具有普遍的应用性。 借助这些组件,不仅可以高效地进行持久化对象的读取与存储,还能够实现事务管理功能。 接下来将通过图形化的方式,逐一阐述这五个核心组件的具体细节。 依据所提供的文件内容,可以总结出以下几个关键知识点:### 1. SSH框架详细架构图尽管标题提及“SSH框架详细架构图”,但在描述部分并未直接呈现关于SSH的详细内容,而是转向介绍了Hibernate的核心接口。 然而,在此我们可以简要概述SSH框架(涵盖Spring、Struts、Hibernate)的核心理念及其在Java开发中的具体作用。 #### Spring框架- **定义**:Spring框架是一个开源架构,其设计目标在于简化企业级应用的开发流程。 - **特点**: - **分层结构**:该框架允许开发者根据实际需求选择性地采纳部分组件,而非强制使用全部功能。 - **可复用性**:Spring框架支持创建可在不同开发环境中重复利用的业务逻辑和数据访问组件。 - **核心构成**: - **核心容器**:该部分包含了Spring框架的基础功能,其核心在于`BeanFactory`,该组件通过工厂模式运作,并借助控制反转(IoC)理念,将配置和依赖管理与具体的应用代码进行有效分离。 - **Spring上下文**:提供一个配置文件,其中整合了诸如JNDI、EJB、邮件服务、国际化支持等企业级服务。 - **Spring AO...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值