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 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 10 12 ) 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

题意:
根据面积求矩形地毯的形状数目,非正方形,并且限制了最小边的长度

思路:
将面积分解质因数,求出所有可能,在减去限制的可能种数,直接分解每种情况会超时,所以先打素数表,所有的情况数等于质因数的幂指数的乘积,即求出质因数的幂指数

#include<iostream>
#include<cstdio>
#include<cstring> 
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<queue>
#include<ctime>
#define INF 0x3f3f3f3f
using namespace std;

int p[1000005],k;

void prime(void){//打表素数
    k = 0;
    bool s[1000005];
    memset(s,1,sizeof(s));
    for(int i=2; i<1000004; i++){
        if(s[i]){
            p[k++] = i;
            for(int j=i; j<1000004; j+=i){
                s[j] = false;
            } 
        }
    }
}

long long int nop(long long int n){//求所有的种类数
    long long int res = 1;
    for(int i=0; i<k && p[i]<=n; i++){
        int m = 1;//当前质因数的幂指数
        while(n%p[i] == 0){
            m++;
            n/=p[i];
        }
        res*=m;
    }
    if(n>1) res*=2;
    return res;
}

int main(){
    prime();
    int t, count = 1;
    scanf("%d",&t);
    while(t--){
        long long int m,n;
        scanf("%lld%lld",&n,&m);
        if(m*m > n){
            printf("Case %d: 0\n",count++);
        }
        else{
            long long int res = nop(n)/2;
            int cent = 0;//最小边长小于限制的边长的情况
            for(int i=1; i<m; i++){
                if(n%i == 0) cent++;
            } 
            res -= cent;
            printf("Case %d: %lld\n",count++,res);
        }
    }       
}

猜你喜欢

转载自blog.csdn.net/qinying001/article/details/82382311
今日推荐