解题报告:LightOJ 1341 Aladdin and the Flying Carpet 唯一分解+DFS

本文介绍了一个算法挑战题目,需要求解给定面积和最小边长条件下可能的长宽组合数量。通过预处理素数表并进行深度优先搜索来解决此问题。
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





题意:
T组数据,每组给定矩形面积大小a,和边的最小值b,询问有多少组满足要求的长宽组合,注意长宽不能相等


思路:
看数据范围就知道不能暴力,预处理sqrt(a)的素数表,先得到面积的唯一分解式,然后dfs满足要求的长宽的组合即可,看有的人的代码用暴力过得,只能说数据太水了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>

#define GET_TIME7

#ifdef GET_TIME
#include<ctime>
#endif // GET_TIME

using namespace std;

int m=0;
int pr[80000];
bool isp[1000005];

int num[100];
int eum[100];



inline void init(){
    memset(isp,0,sizeof(isp));
    int n = 1e6;
    for(int i=2;i<=n;i++){
        if(!isp[i]){pr[m++]=i;
            if(i<=1000)
            for(int j=i*i;j<=n;j+=i){
                isp[j] = true;
            }
        }
    }
}

int all ;
long long ans ;
long long n,mi;
int k ;

inline void oper(long long n){
    k = (int)sqrt(n+0.5);
    ans = 0;
    if(1LL*k*k==n&&mi<=k){
        ans--;
    }
    all = -1;memset(num,0,sizeof(num));
    for(int i=0;i<m;i++){
        if(pr[i]>k||n<=1){
            break;
        }
        if(n%pr[i]==0){
            eum[++all]=pr[i];

            num[all]++;
            n/=pr[i];
            while(n%pr[i]==0){
                num[all]++;
                n/=pr[i];
            }
        }
    }if(n>1){
        eum[++all]=n;
        num[all]=1;
    }
}

void dfs(int mul,int id){
    if(mul>k)return ;
    if(id==all+1){
        if(mul>=mi&&n/mul>=mi){
            ans++;
        }
        return ;
    }
    for(int j=0;j<=num[id];j++){
        if(j)mul*=eum[id];
        dfs(mul,id+1);
    }
    return ;
}

inline void check(){
    for(int i=0;i<=all;i++){
        printf("%d : %d ---- > %d \n",i,eum[i],num[i]);
    }
}

int main()
{
    init();
    int T,t=0;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld",&n,&mi);
        oper(n);
        //check();
        if(mi<=k){
            dfs(1,0);
        }

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

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值