LightOJ 1341 Aladdin and the Flying Carpet (唯一分解定理+素数筛)

在光之OJ-1341问题中,我们探讨了Aladdin如何使用神奇飞毯解决谜题并进入魔法洞穴。故事背景是Aladdin在邪恶巫师的引导下发现了神奇飞毯,用以避开洞口的守卫。任务是在给定飞毯面积和最短边长的情况下,找出可能的飞毯形状数量。本问题涉及数学原理,如唯一分解定理,以及编程技巧,如素数筛法和因子计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Aladdin and the Flying Carpet

LightOJ - 1341

Problem

It’s said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery.

Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised himself as Aladdin’s uncle, found a strange magical flying carpet at the entrance. There were some strange creatures guarding the entrance of the cave. Aladdin could run, but he knew that there was a high chance of getting caught. So, he decided to use the magical flying carpet. The carpet was rectangular shaped, but not square shaped. Aladdin took the carpet and with the help of it he passed the entrance.

Now you are given the area of the carpet and the length of the minimum possible side of the carpet, your task is to find how many types of carpets are possible. For example, the area of the carpet 12, and the minimum possible side of the carpet is 2, then there can be two types of carpets and their sides are: {2, 6} and {3, 4}.

Input

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

Each case starts with a line containing two integers: a b (1 ≤ b ≤ a ≤ 1012) where a denotes the area of the carpet and b denotes the minimum possible side of the carpet.

Output

For each case, print the case number and the number of possible carpets.

Sample Input

2

10 2

12 2

Sample Output

Case 1: 1

Case 2: 2

题意:

给出矩形的面积和组成该矩形的边的最小值,问能组成多少满足条件的矩形。

例如样例12 和 2,共有两种(2,6)和(3,4)。

思路:

唯一分解定理(算术基本定理):任何一个大于1的自然数N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积。

N = P 1 a 1 ∗ P 2 a 2 ∗ P 3 a 3 . . . P n a n N=P1 ^{a1}*P2^{a2}*P3^{a3}...Pn^{an} N=P1a1P2a2P3a3...Pnan
这里P1<P2<P3…<Pn均为质数,其中指数ai是正整数。这样的分解称为N的标准分解式。

①它的正因数的个数为(1+a1)(1+a2)(1+a3)…(1+an)。

②它的全体正因数之和为
s u m = ( 1 + p 1 + p 1 2 + . . . + p 1 a 1 ) ( 1 + p 2 + p 2 2 + . . . p 2 a 2 ) . . . ( 1 + p n + p n 2 + . . . + p n a n ) sum=(1+p1+p1^{2}+...+p1^{a1})(1+p2+p2^{2}+...p2^{a2})...(1+pn+pn^{2}+...+pn^{an}) sum=(1+p1+p12+...+p1a1)(1+p2+p22+...p2a2)...(1+pn+pn2+...+pnan)

这个题使用唯一分解定理。先进行素数打表,根据打表后的素数算出多少次方,然后代入公式,求出a的正因数个数。在暴力求出1-b内的a的因子个数,相减一下就好了。

代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
int prime[maxn];
bool is_Prime[maxn];
int tot = 0;
void GetPrime()
{
    memset(is_Prime, false, sizeof(is_Prime));
    for(int i = 2; i <= maxn ; i++) {
        if(!is_Prime[i])
            prime[tot++] = i;
        for(int j = i+i; j < maxn; j+=i) {
            is_Prime[j] = 1;
        }
    }
}
ll solve(ll x)
{
    ll sum = 1;
    for(int i = 0; i < tot && prime[i]*prime[i] <= x; i++) {
        if(prime[i]>maxn) break;
        if(x % prime[i] == 0) {
            ll cnt = 0;

            while(x % prime[i] == 0) {
                cnt++;
                x /= prime[i];
            }
            sum *= (cnt+1);
        }
    }
    if(x > 1)
        sum *= 2;
    return sum;
}
int main()
{
    int t;
    scanf("%d", &t);
    GetPrime();
    for(int i = 1; i <= t; i++) {
        ll a, b;
        scanf("%lld%lld", &a, &b);
        if(b >= sqrt(a))
            printf("Case %d: 0\n",i);
        else {
            ll count = solve(a) / 2;
            for(int j = 1; j < b; j++) {
                if(a % j == 0)
                    count--;
            }
            printf("Case %d: %lld\n",i,count);
        }

    }

    return 0;
}


内容概要:本文档为《400_IB Specification Vol 2-Release-2.0-Final-2025-07-31.pdf》,主要描述了InfiniBand架构2.0版本的物理层规范。文档详细规定了链路初始化、配置与训练流程,包括但不限于传输序列(TS1、TS2、TS3)、链路去偏斜、波特率、前向纠错(FEC)支持、链路速度协商及扩展速度选项等。此外,还介绍了链路状态机的不同状态(如禁用、轮询、配置等),以及各状态下应遵循的规则和命令。针对不同数据速率(从SDR到XDR)的链路格式化规则也有详细说明,确保数据包格式和控制符号在多条物理通道上的一致性和正确性。文档还涵盖了链路性能监控和错误检测机制。 适用人群:适用于从事网络硬件设计、开发及维护的技术人员,尤其是那些需要深入了解InfiniBand物理层细节的专业人士。 使用场景及目标:① 设计和实现支持多种数据速率和编码方式的InfiniBand设备;② 开发链路初始化和训练算法,确保链路两端设备能够正确配置并优化通信质量;③ 实现链路性能监控和错误检测,提高系统的可靠性和稳定性。 其他说明:本文档属于InfiniBand贸易协会所有,为专有信息,仅供内部参考和技术交流使用。文档内容详尽,对于理解和实施InfiniBand接口具有重要指导意义。读者应结合相关背景资料进行学习,以确保正确理解和应用规范中的各项技术要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值