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

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

  这道题的大致意思是给你 T 行,每行有两个数字 a ,b。其中a 代表一个矩阵的面积 ,然后 b 代表着形成面积 a 这个矩形的最小边边长。
  这道题题目意思很好理解,一开始我感觉就是暴力去做,交的时候感觉没有这么简单,果不其然 TAT 了。然后就在T的道路上一发不可收拾。最后实在是想不出来的时候,就上网看大佬的博客,发现是用唯一分解定理(算术基本定理)来做的,然后我就wa的一下哭了出来,还是太菜了。一开始学因数的时候,算数基本定理看过,但只是看过一眼,脑子里也记得是怎么回事,但是做题目的时候还是不会,题目还是做得少。

算术基本定理

1).对于任意的大于1的自然数N,都有 N = p1^a1 * p2^a2 * p3^a3……pn^an(p1,……
pn代表素数,a1,a2……an代表N中ai的个数),列如:12=2^2 *3^1,p1=2,a1=2,p2=3
,a2=12)N的正约数个数=(1+a1)*(1+a2)*(1+a3)*……*1+an).
3)N的所有正约数和为:(1+p1+p1^2+……+p1^c1)*(1+p2+p2^2+……+p2^c2)*……*
(1+pn+pn^2+……+pn^cn).

知道算术基本定理,这道题就比较简单了。

具体代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
const int mode=1e9+7; 
typedef long long LL;
const int maxn=1e6+100;
int prime[maxn];
int v[maxn];
int m;
void init(){   //筛选素数 
	for(int i=0;i<maxn;i++){
		v[i]=0;
	}
	m=0;
	for(int i=2;i<maxn;i++){
		if(!v[i])prime[++m]=i;
		for(int j=1;j<=m;j++){
			if(i*prime[j]>maxn)break;
			v[i*prime[j]]=1;
			if(i%prime[j]==0)break; 
		}
	}
}
LL solve(LL x){    //唯一分解定理 
	LL res=1; 
	if(x==0)return 0;
	for(int i=1;i<=m;i++){
		if(prime[i]>x)break;
		int cnt=0;
		if(x%prime[i]==0){
			while(x%prime[i]==0){  //用于计算prime[i]在 x 中所含的个数 
				x/=prime[i];
				cnt++;
			}
		}
		res=res*(cnt+1);
	}
	if(x>1)res=res*2;
	return res;
}
int main(){
	int t;
	init();
	scanf("%d",&t);
	int Case=0;
	while(t--){
		LL a,b;LL cnt=0;
		scanf("%lld%lld",&a,&b);
		if(b>=sqrt(a))printf("Case %d: %lld\n",++Case,cnt);
		else {
			
			for(LL i=1;i<b;i++){
				if(a%i==0)cnt++;
			}
			LL all=solve(a)/2;
			
			printf("Case %d: %lld\n",++Case,all-cnt);
		}
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值