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 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=pa11pa22...pakk

那么,约数个数
d|n1=(a1+1)(a2+1)...(ak+1)

这个题在卡质因数分解,得先打个质数表以减少分解时的冗余操作

代码

超时代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL __int64
LL p[64],s,k;int N,ans;

void dfs(LL a,LL b,int cnt){
    if(a>=k) ans++;
    for(int i=cnt;i<N;i++){
        if(b%p[i]==0){
            LL tmp=a*p[i],tmpb=b/p[i];
            if(tmpb>=k&&tmp<tmpb){
                dfs(tmp,tmpb,i);
            }
        }
    }
}

void solve(LL n){
    ans=N=0;
    for(LL i=2;i*i<n;i++)
        if(n%i==0){
            p[N++]=i;
            while(n%i==0) n/=i;
        }
    if(n>1) p[N++]=n;
    dfs(1,s,0);
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        scanf("%lld%lld",&s,&k);
        solve(s);
        printf("Case %d: %d\n",t,ans);
    }
    return 0;
}

方法一,对上面代码优化后

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX_N 1000100
#define LL __int64
#define p_i prime[i]
LL p[64],s,k;int N,ans;
bool vis[MAX_N];int prime[MAX_N],pN;

void init(){
    pN=0;
    for(int i=2;i<MAX_N;i++)
        if(!vis[i]){
            prime[pN++]=i;
            for(LL k=(LL)i*i;k<MAX_N;k+=i)
                vis[k]=true;
        }
}

void dfs(LL A,LL B,int cnt){
    if(A>=k) ans++;
    for(int i=cnt;i<N;i++){
        if(B%p[i]==0){
            LL a=A*p[i],b=B/p[i];
            if(b>=k&&a<b){
                dfs(a,b,i);
            }
        }
    }
}

void solve(LL n){
    ans=N=0;
    for(int i=0;(LL)p_i*p_i<=n;i++)
        if(n%p_i==0){
            p[N++]=p_i;
            while(n%p_i==0) n/=p_i;
        }
    if(n>1) p[N++]=n;

    dfs(1,s,0);
}

int main()
{
//    FILE *fp;
//    fp=fopen("out.txt","wt+");

    int T;init();
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        scanf("%lld%lld",&s,&k);
        if(s==1&&k==1) ans=0; //特殊情况
        else solve(s);

        printf("Case %d: %d\n",t,ans);
//        fprintf(fp,"Case %d: %d\n",t,ans);
    }
    return 0;
}

方法二,使用上面提到的那个公式

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX_N 1000100
#define LL __int64
#define p_i prime[i]
LL p[64],s,k;int N,ans;
bool vis[MAX_N];int prime[MAX_N],pN;

void init(){
    pN=0;
    for(int i=2;i<MAX_N;i++)
        if(!vis[i]){
            prime[pN++]=i;
            for(LL k=(LL)i*i;k<MAX_N;k+=i)
                vis[k]=true;
        }
}

void dfs(LL A,LL B,int cnt){
    ans--;
    for(int i=cnt;i<N;i++){
        if(B%p[i]==0){
            LL a=A*p[i],b=B/p[i];
            if(a<b&&a<k){
                dfs(a,b,i);
            }
        }
    }
}

void solve(LL n){
    ans=1;N=0;
    for(int i=0;(long long)p_i*p_i<=n;i++)
        if(n%p_i==0){
            p[N++]=p_i;
            int tmp=1;
            while(n%p_i==0) n/=p_i,tmp++;
            ans*=tmp;
        }
    if(n>1) p[N++]=n,ans*=2;
//    LL tmp=sqrt(s);
//    if(tmp*tmp==s) ans++;
    ans/=2;

    dfs(1,s,0);
    if(k==1) ans++;
}

int main()
{
    int T;init();
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        scanf("%lld%lld",&s,&k);
        solve(s);
        printf("Case %d: %d\n",t,ans);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值