Aladdin and the Flying Carpet LightOJ - 1341(算数基本定理)

题目链接:qaq

题意:找出符合的因子对数的个数

思路:知道算数基本定理后就简单了(感觉和暴力的复杂度一样啊QAQ。。难受)

附上代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int MAXN = 1000100;
int prime[MAXN + 1];
long long sum=1;
void getPrime() {
	memset(prime, 0, sizeof(prime));
	for (int i = 2; i <= MAXN; i++) {
		if (!prime[i])prime[++prime[0]] = i;
		for (int j = 1; j <= prime[0] && prime[j] <= MAXN / i; j++) {
			prime[prime[j] * i] = 1;
			if (i%prime[j] == 0) break;
		}
	}
}
long long factor[100][2];
int fatCnt;
void getFactors(long long x) {
	fatCnt = 0;
	long long tot=0;
	long long tmp = x;
	for (int i = 1; prime[i] <= tmp / prime[i]; i++) {
		factor[fatCnt][1] = 0;
		tot=0;
		if (tmp%prime[i] == 0) {
			factor[fatCnt][0] = prime[i];
			while (tmp%prime[i] == 0) {
				factor[fatCnt][1]++;
				tmp /= prime[i];
				tot++;
			}
			sum*=(tot+1);
			fatCnt++;
		}
	}
	if (tmp != 1) {
		factor[fatCnt][0] = tmp;
		factor[fatCnt++][1] = 1;
		sum*=2;
	}
}
int main (void){
    int t;
    scanf("%d",&t);
    int ca=1;
    getPrime();
    while(t--){
        sum=1;
        long long n,m;
        scanf("%lld%lld",&n,&m);
        printf("Case %d: ",ca++);
        if(m>sqrt(n)){
            printf("0\n");
            continue;
        }
        getFactors(n);
    //printf("%d\n",sum);
        sum=sum/2;
        for(int i=1;i<m;i++){
            if(n%i==0) sum--;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liexss/article/details/84111545