Aladdin and the Flying Carpet ??算术基本定理

题目链接:http://lightoj.com/volume_showproblem.php?problem=1341

参考博客: https://blog.csdn.net/u012860063/article/details/44784031?from=singlemessage

https://blog.csdn.net/ydd97/article/details/47832293

题意:

给出整数 a 和 b ,求区间[b, a] 内的 a 的约数对的个数,即:满足c*d == a 且 c>=b,d>=b。a 的约数对(比如[2, 3] 与 [3, 2] 为同一对)。
先素数打表一下,然后再运用算术基本定理中的(1)
即可求出正因数的个数,然后再除以2,便是对数,最后再暴力求解出[1,b]中 a 的正因数个数,相减便是答案!

勉强过了

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN=1000000;// 1 ≤ b ≤ a ≤ 10^12 素数打表的时候用,只需要一半就行 
int prime[MAXN];//标记素数位置作用 
int isp[MAXN];//标记是不是素数 
int tot;
typedef long long LL;
//素数打表 
void isprime(){
	tot=0;
	isp[1]=1;//0?
	for(int i=2;i<MAXN;i++)
	{
		if(!isp[i]){
			prime[tot++]=i;//tot统计素数个数
			for(int j=i+i;j<MAXN;j+=i)//??没有= MAXN就不会超时为什么 
			{
				isp[j]=1;//?0
			}
		}
	}
}//为什么标记变了的就不对?? 
//算术基本定理 性质1 1——n正因子个数num 
LL solve(LL n){
	LL sol=1;
	for(int i=0;i<tot&&n;i++){
		LL num=0;
		if(prime[i]>n) break;
		while(n%prime[i]==0){
			n/=prime[i];//p是素数 标准分解式分解 求得公式中的a1,a2 ……an 
			num++;
		}
		sol*=(1+num);//正因数个数(1+a1)(1+a2)(1+a3)……(1+an) 
	} 
	if(n>1)sol*=(1+1);//?大于1的正整数 
	return sol;
}

int main(){
	isprime();
	LL a,b;
	int t;
	cin>>t;
	int k=1;//k记录案例个数 
	while(t--){
		scanf("%lld%lld",&a,&b);
		if(b*b>=a){
			printf("Case %d: 0\n",k++);
			continue;
		}
		LL num1=solve(a);//求正因子个数num1 
        LL num2=0; 
		LL num;
		num1/=2;//约数对 
		//num为奇数时直接除2 刚好减去了正方形的情况。例如9 3 3减去一个3 
		//然后在枚举1-b 中属于a正因子的数 num2
		for(LL i=1;i<b;i++)
			if(a%i==0)num2++;
			num=num1-num2;//[b, a] 内的 a 的约数对的个数 
		printf("Case %d: %lld\n",k++,num);
	}
	return 0;
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值