lightOJ1341 Aladdin and the Flying Carpet(唯一分解)

来源:互联网 发布:阿里分布式数据库 编辑:程序博客网 时间:2024/06/05 01:16

Aladdin and the Flying Carpet
Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1341 uDebug
Appoint description:

Description

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 b denotes 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

题解:
这道题的要求就是给你n,m,然后要你求n的因子中大于等于m的数量。

这道题需要使用唯一分解定理:

任何一个大于1的自然数N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1a1P2a2P3a3......Pnan,这里P1<P2<P3......<Pn均为质数,其中指数ai是正整数。这样的分解称为N的标准分解式

那么,对于每一个pi,我可以选择 选或者不选,选的话我可以选x个。
那么如果一个pi有ai个,那么我就有ai+1种选择。
那么我们就减去小于m的就行了。

可以直接枚举m,因为m不会大于sqrt(n),不然就直接为0了。

#include<bits/stdc++.h>using namespace std;vector<int> pri;int pl;bool Judge(int now) {    if(now==1)return false;    int up=(int)sqrt(now*1.00);    for(int i=2; i<=up; ++i) {        if(now%i==0)return false;    }    return true;}long long solve(long long n) {    int nowl=0;    long long t=2,ans=1;    while(nowl<pl&&n>1) {        int nums=0;        while(n%pri[nowl]==0) {            ++nums;//            cout<<pri[nowl]<<" ";            n/=pri[nowl];        }        ++nowl;        ans*=(nums+1);    }//    cout<<" | "<<n<<endl;    if(n>1)ans<<=1;    ans>>=1;    return ans;}long long work(long long n,long long m) {    long long ans=0;    for(int i=1; i<m; ++i) {        if(n%i==0)++ans;    }    return ans;}void preSolve() {    pri.clear();    for(int i=2; i<=1e6; ++i) {        if(Judge(i))pri.push_back(i);    }    pl=(int)pri.size();}int main() {//    cout<<(long long)(1e9+7)*7*7*3<<endl;    preSolve();    int T,Tc=0;    scanf("%d",&T);    while(T--) {        long long n,m,ans=1;        scanf("%lld%lld",&n,&m);        long long sn=(long long)sqrt(n*1.00);        if(m>sn) {            ans=0;        } else {            ans=solve(n)-work(n,m);        }        printf("Case %d: %lld\n",++Tc,ans);    }    return 0;}
1 0