zoukankan      html  css  js  c++  java
  • Aladdin and the Flying Carpet(算术基本定理)

     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

    题意:给个矩形的面积a,和矩形的最小边长b,问有多少种矩形的方案(不能是正方形)

    分析:a可以写成x,y,因为不能是正方形,所以设x<y,那么x<sqrt(a),y>sqrt(a)

            所以找到所有小于sqrt(a)的因子,看有几个大于等于b的就是方案数

            因子可以由数的唯一分解定理,求得

    具体 : 先筛一遍1e6以内的素数,有线性筛,然后分解a,然后dfs找所有的小于sqrt(a)的因子,

              由于前12个素数的乘积大于1e12了,所以这部分复杂度,大概是O(2^12)(一般还要略大,不过大不了多少,数组要开大)左右

             可以用这个估计(因为是求小于sqrt(a)的,可以除以2,当然这是空间常数)

              所以这部分复杂度是O(T*2^12)满的话(4000*4000)大概也就是几百万,这部分可以忽略不计

              主要的复杂度在分解素数里,因为1e6里面大概有7w多素数,这部分复杂度(最坏的情况a是大素数),大概是4000*70000,可以卡过,由于不可能都是这种数据

              所以还是可以过的

    吐槽:然后我看了看网上的代码,都是先求出总的,然后暴力扫b减,结果居然过了,b是sqrt(a)的级别,是百万,4000*1e6,是4e9,TLE

            出题人太良心,没有卡这种的QAQ,感觉略坑啊


    #include <cstdio>
    #include <iostream>
    #include <ctime>
    #include <vector>
    #include <cmath>
    #include <map>
    #include <queue>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    const int N=1e6+5;
    const int INF=0x3f3f3f3f;
    int cnt;
    bool v[N];
    LL prime[80000];
    
    void getprime()
    {
        for(int i=2; i*i<=N-5; ++i)
            if(!v[i])
                for(int j=i*i; j<=N-5; j+=i)
                    v[j]=1;
        for(int i=2; i<=N-5; ++i)
            if(!v[i])
                prime[++cnt]=i;
    }
    vector<LL>fac[2];
    int divisors[5000],tot;
    LL k;
    
    
    void dfs(int pos,LL res)
    {
        if(pos==fac[0].size())
        {
            divisors[++tot]=res;
            return;
        }
        for(LL i=0,now=1; i<=fac[1][pos]; now*=fac[0][pos],++i)
        {
            if(now*res>=k)
                break;
            dfs(pos+1,res*now);
        }
    }
    
    
    int main()
    {
        getprime();
        int cas=0,T;
        scanf("%d",&T);
        while(T--)
        {
            printf("Case %d: ",++cas);
            LL a,b;
            scanf("%lld%lld",&a,&b);
            k=sqrt(a);
            if(k*k!=a)++k;
            if(b>=k)
            {
                printf("0
    ");
                continue;
            }
            LL t=a;
            fac[0].clear(),fac[1].clear();
            for(int i=1; i<=cnt&&prime[i]*prime[i]<=t; ++i)  ///质因数分解
            {
                if(t%prime[i])
                    continue;
                int tmp=0;
                fac[0].push_back(prime[i]);
                while(t%prime[i]==0)
                    ++tmp,t/=prime[i];
                fac[1].push_back(tmp);
            }
            if(t>1)
            {
                fac[0].push_back(t);
                fac[1].push_back(1);
            }
            tot=0;
            dfs(0,1);
            int ans=0;
            for(int i=1; i<=tot; ++i)
                if(divisors[i]>=b)
                    ++ans;
            printf("%d
    ",ans);
        }
        return 0;
    }

    题目大意:
    给定T组数据,每组数据有两个数 面积s 和 矩形的宽 a,让你求的是在面积s一定的情况下,假设长和宽分别为a 和 b,最小的边长 >= a的有几种方式可以组成矩形(不是正方形)

    解析一下样例:
    面积为12 ,最小的边长为2:
    首先将12进行素因子分解12 = 2^2*3,所以我们能够得到 
    12 = 1 * 12(不符合条件 最小的边长<2)
    12 = 2 * 6(符合)
    12 = 3 * 4(符合)
    所以有 2 种方式,输出 2


    解题思路:
    每次做题的时候先要考虑一下能不能暴力(暴力简单),这个题如果我们要暴力的话肯定会超时,所以就不要暴力了
    通过上述的样例分析,我们也知道了我们首先要做的就是将 面积 s 进行素因子分解,这就用到了唯一分解定理,
    s = p1^e1 * p2^e2 *……* pk^ek,我们要得到的是因子的个数,这里所说的因子个数默认为正的,得到因子个数的方法是 num = (e1+1) * (e2+1) * ... *(ek+1),然后又因为没有正方形,而且我们要得到的是有多少对,所以将 num除以2,就得到了可以组成矩形面积为 s 的矩形个数,然后我们只需要在 [1,a)的区间内(注意区间开闭)将 s 的因子减掉就行了(num--),这样就可以了。
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e6+5;
    bool prime[MAXN];
    LL p[MAXN],k;
    void isprime()
    {
        memset(prime, true, sizeof(prime));
        prime[1] = false;
        k = 0;
        for(LL i=2; i<MAXN; i++)
        {
            if(prime[i])
            {
                p[k++] = i;
                for(LL j=i*i; j<MAXN; j+=i)
                    prime[j] = false;
            }
        }
    }
    
    LL Solve(LL m)
    {
        LL ret = 1;
        for(LL i=0; p[i]*p[i]<=m&&i<k; i++)
        {
            LL cnt = 0;
            if(m%p[i] == 0)
            {
                while(m%p[i] == 0)
                {
                    cnt++;
                    m /= p[i];
                }
                ret *= (cnt+1);
            }
        }
        if(m > 1)
            ret *= 2;
        return ret;
    }
    int main()
    {
        isprime();
        int T;
        cin>>T;
        for(int cas=1; cas<=T; cas++)
        {
            LL s,a;
            cin>>s>>a;
            if(a*a >= s)
                printf("Case %d: 0
    ",cas);
            else
            {
                LL ret = Solve(s);
                ret /= 2;
                for(LL i=1; i<a; i++)
                    if(s % i == 0)
                        ret--;
                printf("Case %d: %lld
    ",cas,ret);
            }
        }
        return 0;
    }
    



  • 相关阅读:
    java开发——Cloneable接口、clone()方法和深浅拷贝
    intellij idea 显示Arraylist 扩容过程 解决not showing null elements
    ArrayList的扩容方式和扩容时机
    【laravel5.4】发送alisms短信和163邮箱
    【laravel5.4】重定向带参数
    【laravel5.4】vue分页删除
    【laravel5.4】Baum无限极分类和collect助手函数、transform()中间件(转换数据)方法使用
    【laravel5.4】使用baum ode 类库实现无限极分类
    【VUE+laravel5.4】vue给http请求 添加请求头数据
    【laravel5.4】关键字【use】使用
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717947.html
Copyright © 2011-2022 走看看