zoukankan      html  css  js  c++  java
  • LightOJ 1341

    http://lightoj.com/volume_showproblem.php?problem=1341

    Aladdin and the Flying Carpet
    Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

    Description

    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: ab(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

    题目大意:给出矩形面积ab,和组成该矩形的边的最小值,问这种面积为ab的矩形有几种

    比如样例12 2,矩形面积为12,组成这样矩形的最小边为2,共有2种这样的矩形(2, 6),(3, 4)(这些边都大于或等于2,其中(2,6)和(6,2)是同一种)

    这道题用到了唯一分解定理:N = p1^a1*p2^a2*p3^a3* ... *pn^an(其中p1、p2、... pn为N的因子,a1、a2、... 、an分别为因子的指数)

    N的因子个数     M = (1 + a1)*(1 + a2)*(1 + a3)*...*(1 + an);

    用唯一分解定理求出ab的因子个数,但题要求的是满足条件的因子对数,所以最终所求的因子个数需要除以2,然后再将不满足的减去

    该题要用到筛选素数来缩短时间(减少循环次数)来防止TLE

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    
    using namespace std;
    
    typedef long long ll;
    const int N = 1e6 + 10;
    
    int prime[N], k;
    bool Isprime[N];
    
    void Prime()
    {
        k = 0;
        memset(Isprime, true, sizeof(Isprime));
        Isprime[1] = false;
        for(int i = 2 ; i < N ; i++)
        {
            if(Isprime[i])
            {
                prime[k++] = i;
                for(int j = 2 ; i * j < N ; j++)
                    Isprime[i * j] = false;
            }
        }
    }//素数筛选
    
    ll solve(ll n)
    {
        ll ans = 0, sum = 1;
        for(ll i = 0 ; i < k && prime[i] * prime[i] <= n ; i++)
        {
            if(n % prime[i] == 0)
            {
                ans = 0;
                while(n % prime[i] == 0)
                {
                    ans++;
                    n /= prime[i];
                }
                sum *= (1 + ans);
            }
        }
        if(n > 1)
            sum *= 2;
        return sum;
    }//找n的因子个数
    
    int main()
    {
        Prime();
        int t, x = 0;
        ll ab, a, num;
        scanf("%d", &t);
        while(t--)
        {
            x++;
            scanf("%lld%lld", &ab, &a);
            if(ab < a * a)
            {
                printf("Case %d: 0
    ", x);
                continue;
            }
            num = solve(ab);
            num /= 2;
            for(ll i = 1 ; i < a ; i++)
                if(ab % i == 0)
                num--;//将边小于a的情况减去
            printf("Case %d: %lld
    ", x, num);
        }
        return 0;
    }
     
  • 相关阅读:
    c++拷贝构造函数和赋值运算符
    c++运算符定义为成员函数还是非成员函数
    c++重载运算符位置的限制
    为什么operator<<运算符重载一定要为友元函数
    动态规划求一定数量骰子和的概率
    vector之reserve的坑
    c++ decltype和auto对比学习
    asio的前摄器模式
    动态显示当前时间
    js遍历二维数组
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4924216.html
Copyright © 2011-2022 走看看