Fundamental theorem of arithmetic

题目描述:
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 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.
输出:
For each case, print the case number and the number of possible carpets.
输入样例:
2

10 2

122
Output Sample:
Case. 1:. 1

Case 2: 2

Description Title:
recording a can be larger than the number divisible by an integer equal to b.
Knowledge:
the fundamental theorem of arithmetic.
Problem-solving ideas:
The Unique Factorization Theorem, a first unique factorization, the number of all of a positive number is about num = (1 + a1) * (1 + a2) * ... (1 + ai), ai where is the index prime factor, see the only decomposition theorem, because there will be no case where a topic that c == d, so num 2 to remove duplication, then the enumerator less than a divisor of b, take other num lose it
Code:

#include<stdio.h>
#include<string.h>
const int max=1000050;
int prim[1000050],f[1000050];
long long k;
void find()//素数打表
{
    k=0;
    memset(f,0,sizeof(f));
    f[0]=1;f[1]=1;
    for(int i=2;i<=max;i++)
    {
        if(!f[i])
        {   
            prim[k++]=i;//存储素数
            for(int j=i*2;j<=max;j+=i)//翻倍递增
                f[j]=1;//标记非素数
        }
    }
}
long long ff(long long a)//求因子个数
{
    long long s=1,i=0;
    while(prim[i]<a&&i<k)
    {
        int sum=0;
        while(a%prim[i]==0)
        {
            a=a/prim[i];
            sum++;//记录能被整除的次数(次方数)
        }
        s=s*(sum+1);//根据算法的基本定理叠乘
        i++;
    }
    if(a>1)//当a大于一时,没被整除,说明还有一个素数,sum(次方)=1
        s=s*2;
    return s;
}
int main()
{
    int t,l=0;
    find();//素数打表
    scanf("%d",&t);
    while(t--)
    {
        l++;
        long long a,b,i,j,m,n=0,s;
        scanf("%lld %lld",&a,&b);
        if(b*b>a)
        {
            printf("Case %d: 0\n",l);
            continue;
        }
        s=ff(a)/2;
        for(i=1;i<b;i++)//暴力枚举
        {
            if(a%i==0)
                s--;
        }
        printf("Case %d: %lld\n",l,s);
    }
    return 0;
}

Published an original article · won praise 0 · Views 15

Guess you like

Origin blog.csdn.net/mashizuren/article/details/105371991