LightOJ - 1341 - Aladdin and the Flying Carpet(唯一分解定理)

链接:

https://vjudge.net/problem/LightOJ-1341

题意:

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}.

思路:

先将分解成质数的幂次形式,求出所有的约数,再减去小于b的约数对即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e9+7;

LL IsPrime[MAXN], Prime[MAXN];
int tot;
LL a, b;

void Init()
{
    memset(IsPrime, 0, sizeof(IsPrime));
    IsPrime[1] = 1;
    tot = 0;
    for (int i = 2;i < MAXN;i++)
    {
        if (IsPrime[i] == 0)
            Prime[++tot] = i;
        for (int j = 1;j <= tot && i*Prime[j] < MAXN;j++)
        {
            IsPrime[i*Prime[j]] = 1;
            if (i%Prime[j] == 0)
                break;
        }
    }
}

int main()
{
    Init();
    int t, cnt = 0;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cnt);
        scanf("%lld%lld", &a, &b);
        if (b*b > a)
        {
            printf(" 0\n");
            continue;
        }
        LL psum = 1;
        LL x = a;
        for (int i = 1;i <= tot && Prime[i] <= x;i++)
        {
            if (x%Prime[i] == 0)
            {
                int tmp = 1;
                while(x%Prime[i] == 0)
                {
                    tmp++;
                    x/=Prime[i];
                }
                psum *= tmp;
            }
        }
        if (x>1)
            psum *= 2;
        psum /= 2;
        for (int i = 1;i < b;i++)
        {
            if (a%i == 0)
                psum--;
        }
        printf(" %lld\n", psum);
    }
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/11846249.html