Aladdin and the Flying Carpet(素数打表,唯一分解定理)

Aladdin and the Flying Carpet


t’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
题意
给出你地毯的面积,和能达到的最短边。求长方形地毯有几种可能的情况。地毯的边为整数。
思路
根据唯一分解定理,我们可以所给a的正因数个数。设的得到的正因数个数为N0,
在这里插入图片描述
N0/2再减去从1到b的因子数即为所求。注意不能成为正方形,所给范围是10^12,需要对
10^6素数筛打表,计算N0。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000500   
#define ll long long
int f[N],prim[N],k=0;
void find_prim()
{
    k=0;
    memset(f,0,sizeof(f));
    f[0]=f[1]=1;
    for(ll i=2; i<=N; i++)
    {
        if(!f[i])
        {
            prim[k++]=i;   //记录素数
            for(ll j=i<<1; j<=N; j+=i)
                f[j]=1;    //不是第一个出现,直接标记掉
        }
    }
}
int river(ll a)   //求a的正因子数
{
    ll s=1,count=0,i=0;
    while(a>prim[i]&&i<k)
    {
        count=0;
        if(a%prim[i]==0)
        {
            while(a%prim[i]==0)
            {
                a=a/prim[i];
                count++;
            }
        }
        s*=count+1;   //质因数之积,唯一分解定理
        i++;
    }
    if(a>1)
        s*=1+1;   //最后剩下质因子,为一次幂。
    return s;
}
int main()
{
    int t,cas=0;
    ll a,b;
    find_prim();
    scanf("%d",&t);
    while(t--)
    {
        ll sum;
        scanf("%lld%lld",&a,&b);
        if(a<b*b) sum=0;   //以最短边作正方形都超面积,没戏
        else
        {
            sum=river(a)/2;  //计算的是1~a的因数对数
            for(ll i=1; i<b; i++)
                if(a%i==0)
                    sum--;   //暴力减去1~b的因数对数
        }
        printf("Case %d: %d\n",++cas,sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值