LightOJ1341 Aladdin and the Flying Carpet 约数相关问题

LightOJ1341 Aladdin and the Flying Carpet

标签

  • 约数相关问题

前言

简明题意

  • 给定n,b,求n的>=b的约数的对数。(n<=1e12)

思路

  • n的约数对数=\(d(n)/2\),这个应该是很显然的。如果n是完全平方数那么这个式子不对,但是题目说了只用找矩形而不用找正方形,因此不需要考虑n是完全平方数的情况。
  • n的约数对数求出来了,但是题目要求>=b的约数对数,怎么搞呢?这里我也想了半天没想明白
  • 很多人说直接算\(d(n)\),再暴力算出n的<b的因子数。我想,题目给的数据是b<=1e12,这暴力枚举b就没了呀。然而我还是too native了,显然,如果b>sqrt(n),那么答案就是0,因此,b实际上最大是\(\sqrt{n}=10^6\) ,暴力枚举并不会超时。
  • 但是仍然会T。这里需要两个小优化:
    1. 提前把质数筛出来,质因数分解时直接用筛出来的质数去分解
    2. 质因数分解那里,当n=1时,要直接跳出循环。这个优化加上可以快3000ms(震惊,怎么会快这么多嘛)

注意事项

  • 不要判断b*b<=n而应该判断b<=\(\sqrt n\)。因为会溢出...

总结

  • 我还是too native了

AC代码

#include<cstdio>
#include<cmath>

const int maxn = 1e6 + 10;

bool no_prime[maxn];
int prime[maxn];
int shai(int n)
{
    int cnt = 0;

    for (int i = 2; i <= n; i++)
    {
        if (!no_prime[i])
            prime[++cnt] = i;

        for (int j = 1; j <= cnt && prime[j] * i <= n; j++)
        {
            no_prime[prime[j] * i] = 1;
            if (i % prime[j] == 0) break;
        }
    }
    return cnt;
}

void solve()
{
    int cnt = shai(maxn - 1);

    int t;
    scanf("%d", &t);
    for (int i = 1; i <= t; i++)
    {
        long long n, r;
        int b;
        scanf("%lld%d", &n, &b);
        if (b > sqrt(n))
        {
            printf("Case %d: 0\n", i);
            continue;
        }
        r = n;

        bool tag = 0;
        long long ans = 1;
        for (int i = 1; i <= cnt && 1ll * prime[i] * prime[i] <= r && n != 1; i++)
        {
            int p = prime[i];
            int cnt = 0;
            while (n % p == 0)
                n /= p, cnt++;
            ans *= (cnt + 1);

            if (n <= 1e6 && !no_prime[n])
            {
                tag = 1;
                ans *= 2;
                break;
            }
        }
        if (n != 1 && !tag)
            ans *= 2;
        ans /= 2;

        for (int i = 1; i < b; i++)
            if (r % i == 0) ans--;

        printf("Case %d: %lld\n", i, ans);
    }
}

int main()
{
    freopen("Testin.txt", "r", stdin);
    freopen("Testout.txt", "w", stdout);
    solve();
    return 0;
}

原文地址:https://www.cnblogs.com/danzh/p/11412205.html

时间: 2024-11-08 10:24:48

LightOJ1341 Aladdin and the Flying Carpet 约数相关问题的相关文章

LightOJ Aladdin and the Flying Carpet 1341【算数基本定理+几何】

1341 - Aladdin and the Flying Carpet PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned

C - Aladdin and the Flying Carpet (质因子分解,因子个数)

C - Aladdin and the Flying Carpet 题目链接:https://vjudge.net/problem/LightOJ-1341#author=2018112767 题目大意: 给一对数字 a,b .其中,a表示一个矩形的面积,想知道有多少种整数的边的组合可以组成面积为a的矩形,而且要求矩形的最短的边不得小于b. 解题思路: 先算出a的因子数,然后由公式因子个数num=(q1+1)*(q2+1)......*(qn+1).求出num后再除以2,得到的就是所有面积等于a

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

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

Light OJ 1341 Aladdin and the Flying Carpet

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 hi

LightOJ 1341 - Aladdin and the Flying Carpet(算术基本定理啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 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

LightOJ 1341(Aladdin and the Flying Carpet )算术基本定理

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 hi

E - Aladdin and the Flying Carpet

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 hi

[LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))

题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https://baike.baidu.com/item/%E7%AE%97%E6%9C%AF%E5%9F%BA%E6%9C%AC%E5%AE%9A%E7%90%86/10920095?fr=aladdin 1 #include<cstdio> 2 #include<vector> 3 #includ

LightOJ 1341 - Aladdin and the Flying Carpet【合数分解】

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意: 给出整数 a 和 b ,求区间[b, a] 内的 a 的约数对的个数,a 的约数对(比如[2, 3] 与 [3, 2] 为同一对). 解法: 主要利用公式: 一个整数n可以表示为若干素数乘积: n = p1^a1 * p2^a2*-*pm^am; 则 n 的正因数的个数可以表示为: num = (a1+1)*(a2+1)-(am+1); 代码: #include <st