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

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值