LightOJ-1431 Aladdin and the Flying Carpet (算术基本定理 + 求约数个数)
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 bdenotes the minimum possible side of the carpet.
Output
For each case, print the case number and the number of possible carpets.
Sample Input
2
10 2
12 2
Sample Output
Case 1: 1
Case 2: 2
约数个数/2- 到小于b的所有约数就是答案
b是乘号的左边 左边小于右边,因为不能是正方形,所以 b<sqrt(n);
时间复杂度 (logn+1)*根号n
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 1000047
long long p[MAXN], prime[MAXN]; //这个prime可以存1~MAXN内的素数
int k=0;
void getprime()
{
long long i,j;
memset(p,0,sizeof(p));
memset(prime,0,sizeof(prime));
for(i=2;i<MAXN;i++)
{
if(p[i]==0)
{
prime[k++]=i;
for(j=2*i;j<MAXN;j+=i)
{
p[j]=1;
}
}
}
prime[0]=2;
}
long long factor_cnt(long long n) //这个算约数个数函数只要算√n内的素数就行了
{
long long sum=1;
if(n==0)
return 0;
long long a=0;
long long i=0;
while(prime[i]*prime[i]<=n&&i<k)
{
a=0;
if(n%prime[i]==0)
{
while(n%prime[i]==0)
{
n/=prime[i];
a++;
}
}
sum*=1+a;
i++;
}
if(n > 1)
sum*=1+1;
return sum;
}
int main()
{
long long a, b;
int t;
int cas=0;
getprime();
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
int cnt=0;
long long num,ans;
if(b>=sqrt(a))
ans=0;
else
{
for(long long i=1;i<b; i++)
if(a%i==0)
cnt++;
num=factor_cnt(a)/2;
ans=num-cnt;
}
printf("Case %d: %lld\n",++cas,ans);
}
return 0;
}