阿拉丁和飞毯(唯一分解定理&因数个数求和+素筛)

我是传送门
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.
1325/5000
据说阿拉丁在得到神灯之前必须解决七个谜题,神灯会召唤出一个强大的精灵。这里我们关注的是第一个谜团。

阿拉丁正要进入一个神奇的山洞,由一个化装成阿拉丁叔叔的邪恶巫师带领,在洞口发现了一块奇怪的魔法飞毯。有一些奇怪的生物守卫着洞穴的入口。阿拉丁可以逃跑,但他知道被抓住的可能性很大。所以,他决定使用魔法飞毯。地毯是长方形的,但不是方形的。阿拉丁拿过地毯,搀着它穿过了入口。

现在你得到了地毯的面积和地毯可能的最小边的长度,你的任务是找出有多少种地毯是可能的。例如,地毯的面积12,地毯的最小可能边是2,那么可以有两种类型的地毯,它们的边是:{2,6}和{3,4}。

输入
输入以整数T(≤4000)开始,表示测试用例数。
每种情况都以包含两个整数的一行开始:a b(1≤b≤a≤1012),其中a表示地毯的面积,b表示地毯的最小可能边。
输出
每箱打印箱号和可能使用的地毯数量。

题意如上很清楚。

分析:唯一分解定理:

在这里插入图片描述

任何一个数都可以分解为合数和素数,合数又可以分解为素数,所以素数打好表分解素数,利用定理即可。

AC代码:

#include<stdio.h>
#include<string.h>
long long f[1000010],vis[1000010],k=0;
int prime()
{
	int i,j;
	vis[1]=1;//1必须先标记,循环从2开始 
	for(i=2;i<=1000010;i++)
	{
		if(!vis[i])
		{
			f[k++]=i;
			for(j=i*2;j<=1000000;j+=i)
			vis[j]=1;
		}
	}
}
int main()
{
	long long g,t,n,m,nn,ans,b,i;
	g=1;
	scanf("%lld",&t);
	prime();
	while(t--)
	{
		scanf("%lld%lld",&n,&m);
		long long p=m*m;
		if(m*m>=n)//特殊情况,0个 
		{
		printf("Case %lld: 0\n",g++);
		continue;
		}
		else
	{
		nn=n;
		b=1;
		for(i=0;i<k&&f[i]*f[i]<=n;i++)
		{
			if(n%f[i]==0)
			{
				ans=0;
				while(n%f[i]==0)//唯一分解定理主要部分 
				{
					ans++;
					n=n/f[i];
				}
				b=b*(ans+1);	
			}
		}
		for(int p=1;p<m;p++)//定理找的是所有正因数,题目给出了最小因数m,舍掉更小的 
		{  
			if(nn%p==0)
			b--;		
		}
		printf("Case %lld: %lld\n",g++,b);
	}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值