light_oj 1347 算术基本定理

light_oj 1347 算术基本定理

C - Aladdin and the Flying Carpet
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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: ab(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

题意:给定面积a和最小边长b,求面积为a且宽大于等于b的长方形个数。

思路:显然,直接暴力的话,从b到sqrt(a)枚举a的约数,复杂度o(sqrt(n))*4000组数据,即10^6*4000,超时。

        因此此题需要用到以下数论知识:

    算术基本定理:

      分解素因数:n=(p1^k1)*(p2^k2)*...*(pn*kn).(分解方式唯一)

      n的约数个数为cnt(n)=(1+k1)*(1+k2)*...*(1+kn).

        本题先求出a的约数个数cnt(a),再暴力枚举出[1,b)中a的约数c,cnt/2-c即为答案。

        然而b是10^12,此时注意到b>sqrt(a)时,不再存在符合条件的长和宽,此处剪枝。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

ll a,b;
bool isprime[maxn];
vector<int> prime;

void play_prime()
{
    memset(isprime,1,sizeof(isprime));
    isprime[1]=0;
    for(int i=2;i<maxn;i++){
        if(!isprime[i]) continue;
        for(int j=i+i;j<maxn;j+=i){
            if(isprime[j]) isprime[j]=0;
        }
    }
    for(int i=2;i<maxn;i++)
        if(isprime[i]) prime.push_back(i);
}

int main()
{
    int casen=1;
    int T;cin>>T;
    play_prime();
    while(T--){
        //cin>>a>>b;
        scanf("%lld%lld",&a,&b);
        ll ans=0;
        ll t=a;
        ll cnt=1;
        for(ll i=0;prime[i]<=t&&i<prime.size();i++){
            int k=0;
            while(t%prime[i]==0){
                t/=prime[i];
                k++;
            }
            cnt*=(1+k);
        }
        if(t>1) cnt*=(1+1);
        ll cnt2=0;
        if(b*b>a) ans=0;
        else{
            for(int i=1;i<b;i++){
                if(a%i==0) cnt2++;
            }
            ans=cnt/2-cnt2;
        }
        //cout<<"Case "<<casen++<<": "<<ans<<endl;
        printf("Case %d: %lld\n",casen++,ans);
    }
    return 0;
}
View Code

 

 

posted @ 2015-06-03 00:26  __560  阅读(357)  评论(0编辑  收藏  举报