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 ≤ 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
问题链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341
问题简述: 输入两个整数a,b,求面积为a并且最短边大于b且所有边长均为整数的矩形有多少个
问题分析: 由题意可得求的是a大于b的因子对数(3,4)与(4,3)算一对。由唯一分解定理可得对于任意正整数一定有a=p1ti×p2t2×…,pi为a的质因子。则a的因子个数为k=(t1+1)(t2+1)…(ti+1)
而当b>根号a时矩形个数为0,直接输出0。假设小于b的因子个数为g个,则满足题意的答案数为ans=(k-2g)/2个,具体可看代码及注释
AC通过的C++语言程序如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#define ll long long
#define endl '\n'
#include<ctime>
using namespace std;

const int maxn=1010101;

ll prime[maxn];//素数
ll visit[maxn];
void Prime()//欧拉筛素数
{
    memset(visit,0,sizeof(visit));
    memset(prime,0,sizeof(prime));
    for (int i = 2;i < maxn; i++)
        {
        if (!visit[i])
        {
            prime[++prime[0]] = i;//纪录素数,prime[0]相当cnt,用来计数
        }
        for (int j = 1; j <=prime[0] && i*prime[j] < maxn; j++)
        {
            visit[i*prime[j]] = 1;
            if (i % prime[j] == 0)
            {
                break;
            }
        }
    }
}

int main()
{
    //ios::sync_with_stdio(false);
    Prime();
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        printf("Case %d: ",i);
        if(b>a/b)//如果b>根号a,直接输出0
        {
            printf("0\n");
            continue;
        }
        ll ans=1;
        ll tm=0;//质因子的t次数
        ll k=a;
        for(int j=1;j<=prime[0]&&prime[j]*prime[j]<=k;j++)//质因子的平方小于k,并且j小于素数数量
        {
            if(k%prime[j]==0)//如果是质因子
            {
                tm=0;//初始化幂
                while(k%prime[j]==0)//计算出几次方
                {
                    tm++;
                    k/=prime[j];
                }
                ans*=(tm+1);//因数数量为质因数次方+1的乘合
            }
        }
        //cout<<ans<<endl;
        if(k>1)//只有剩下一个最大质因数以及1的情况,1则不用乘2
            ans*=2;
        for(int j=1;j<b;j++)//减去小于b的因数
            if(a%j==0) ans-=2;
        ans/=2;//最后除与2即剩一对;(3,4)=(4,3)
        printf("%lld\n",ans);
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44012745/article/details/88985573