(唯一分解定理+约数个数求和+素筛)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


据说阿拉丁在得到召唤强大精灵的神灯之前,必须解开七个谜团。这里我们关注的是第一个谜团。
阿拉丁正要进入一个神奇的洞穴,在邪恶的巫师的带领下,他伪装成阿拉丁的叔叔,在入口处发现了一块奇怪的魔法飞毯。有一些奇怪的生物守卫着洞口。阿拉丁可以逃跑,但他知道被抓住的可能性很大。所以,他决定用神奇的飞毯。地毯是长方形的,但不是正方形的。阿拉丁拿起地毯,在它的帮助下,他通过了入口。
现在给你地毯的面积和地毯最小可能边的长度,你的任务是找出有多少种地毯是可能的。例如,地毯的面积为12,且地毯的最小可能边为2,则可以有两种类型的地毯,其边为:{2,6}和{3,4}。
输入
输入以整数T开始(≤ 4000),表示测试用例的数量。
每种情况都从包含两个整数的行开始:AB(1≤ b≤ 一≤ 1012)其中a表示地毯的面积,b表示地毯的最小可能边。
输出
对于每个箱子,打印箱子编号和可能的地毯数量。
样本输入
2
10 2
12 2
样本输出
案例1:1
案例2:2

唯一分解定理

一个数n肯定能被分解成 n = p1{a1}*p2{a2}*…*pn^{an}. 因为一个数肯定是由合数和质数构成的,合数又可以分解成质数和合数,最后递归下去就会变成质数的乘积。
求出数n的因子个数

n = p1{a1}*p2{a2}*…*pn^{an}有一个很简洁的公式:
n的因子个数 = (1+a1​)(1+a2​)…(1+an​)
在这里插入图片描述

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1e6+10;
long long prime[N];
bool isprime[N];
long long p;
long long n,m,sum;
void s()
{
	int  i,j;
	p=0;
	memset(isprime,1,sizeof(isprime));
	isprime[0]=isprime[1]=false;
	for(i=2;i<=N;i++)
	if(isprime[i])
	{
	prime[p++]=i;
	for(j=2;i*j<=N;j++)
	isprime[j*i]=false;
	}
}
int main()
{
	int t,case1=1;
	long long  i;
	scanf("%d",&t);
	s();
	while(t--)
	{
		scanf("%lld%lld",&n,&m);
		long long y;
		y=n;
		if(n<m*m)
		{
		printf("Case %d: 0\n",case1++);	
		continue;
		}
		else
		{
		sum=1;
    	for(i=0;i<p&&prime[i]*prime[i]<=n;i++)
    	{
    		
    			int q=0;
        		while(n%prime[i]==0)
        		{
                q++;
                n=n/prime[i];
                //printf("%d\n",n);
        		}
            	sum=sum*(1+q);//
            	//printf("%d**\n",sum);
		}
		if(n>1)
        sum=2*sum;
		sum=sum/2;
        for(i=1;i<m;i++)
        {
                if(y%i==0)
                    sum--;
        }
            printf("Case %d: %lld\n",case1++,sum);
        }
	}
}
//唯一分解定理:任何一个大于1的自然数 N,如果N不为质数,
//那么N可以唯一分解成有限个质数的乘积
//N=P1^a1 * P2^a2 * P3^a3 ...... Pn^an;
//求出数n的因子个数
//n的因子个数 = (1+a1)*(1+a2)*...*(1+an)
//思路:根据唯一分解定理,先将a唯一分解,
//则a的所有正约数的个数为num = (1 + a1) * (1 + a2) *...(1 + ai),
//这里的ai是素因子的指数,见唯一分解定理,
//因为题目说了不会存在c==d的情况(是长方形),
//因此num要除2,去掉重复情况,然后枚举小于b的a的约数,拿num减掉就可以了


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值