【洛谷】P1072 Hankson 的趣味题 (题解)

P1072 Hankson 的趣味题


题解: 

效果
提交效果图,亲试
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int prime[100001],n,size;
bool notprime[100001];
ll ans,a1,b1,a0,b0,numa0,numb0,numa1,numb1,la,ra,lb,rb,l,r;
void init() {
	notprime[1]=true;
	for(int i=2; i<=50000; i++) {
		if(!notprime[i])
			prime[++size]=i;
		for(int j=1,k; j<=size; j++) {
			k=i*prime[j];
			if(k>50000)break;
			notprime[k]=true;
			if(i%prime[j]==0)break;
		}
	}
}
int main() {
	init();
	scanf("%d",&n);
	while(n--) {
		ans=1;
		bool ok=true;
		scanf("%lld%lld%lld%lld",&a0,&a1,&b0,&b1);
		if(a1>a0||b1<b0) {
			printf("0\n");
			continue;
		}
		for(int i=1; i<=size; i++) {
			if(a0==1&&a1==1&&b0==1&&b1==1)break;
			numa0=numb0=numa1=numb1=0;
			la=lb=l=0,ra=rb=r=1000000;
			while(a0%prime[i]==0) {
				a0/=prime[i];
				numa0++;
			}
			while(b0%prime[i]==0) {
				b0/=prime[i];
				numb0++;
			}
			while(a1%prime[i]==0) {
				a1/=prime[i];
				numa1++;
			}
			while(b1%prime[i]==0) {
				b1/=prime[i];
				numb1++;
			}
			if(numa0<numa1||numb0>numb1) {
				ok=false;
				break;
			}
			la=numa1;
			rb=numb1;
			if(numa0>numa1) 
			{
				ra=numa1;
			}
			if(numb0<numb1) 
			{
				lb=numb1;
			}
			l=max(la,lb);
			r=min(ra,rb);
			if(r<l) {
				ok=false;
				break;
			}
			ans=(ll)ans*(r-l+1);
		}
		if(!(a0==1&&a1==1&&b0==1&&b1==1)) {
			la=lb=l=0,ra=rb=r=1000000;
			if(a1>a0||b1<b0)ok=false;
			if(a1==a0&&a1!=1) ans<<=1;
			if(b1==b0&&b1!=1) ans<<=1;
		}
		if(!ok) ans=0;
		printf("%lld\n",ans);
	}
	return 0;
}

 

posted @ 2020-02-11 20:02  BorisDimitri  阅读(28)  评论(0编辑  收藏  举报