Aladdin and the Flying Carpet 唯一分解定理

来源:互联网 发布:剑灵龙族男捏脸数据 编辑:程序博客网 时间:2024/06/05 02:08
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

算数基本原理

任何一个大于1的自然数
  
,都可以唯一分解成有限个质数的乘积
  
,这里
  
均为质数,其诸指数
  
是正整数。
定理应用:
(1)一个大于1的正整数N,如果它的标准分解式为:
  
那么它的正因数个数为
  

题目大意:给两个数a,b,求满足c*d==a且c>=b且d>=b的c,d二元组对数,(c,d)和(d,c)属于同一种情况

题目分析:根据唯一分解定理先将a唯一分解,则a的所有正约数的个数为cnt = (1 + a1) * (1 + a2) *...(1 + ai)

因为题目说了不会存在c==d的情况,因此cnt要除2,去掉重复情况。

例如20=2^2*5^1,则cnt=(2+1)*(1+1)=6,分别是1 2 4 5 10 20 能组成不重复的cnt/2=3对,分别是(1,20),(2,10),(4,5)

然后枚举小于b的a的约数,拿cnt减掉就可以了

#include<cstdio>#include<cmath>#include<cstring>using namespace std;typedef long long ll;const int N=1000005;bool v[N];int tot=0,ans[N];void getPrime(){    memset(v,true,sizeof(v));    for(int i=2;i<=N;++i)    {        if(v[i])         ans[tot++]=i;       for(int j=0;((j<=tot)&&(i*ans[j]<=N));++j)       {           v[i*ans[j]]=false;           if(i%ans[j]==0) break;       }    }}int cal(ll a){    int cnt=1;    for(int i=0;i<tot&&ans[i]<=sqrt(a);++i)    {        int num=0;        while(a%ans[i]==0)        {            num++;            a/=ans[i];        }        cnt*=num+1;    }    if(a>1) cnt=cnt*2;//如果a是素数,cnt=2;    return cnt;}int main(){    int t,cnt;    ll a,b;    getPrime();    scanf("%d",&t);    for(int i=1;i<=t;++i)    {       scanf("%lld%lld",&a,&b);       if(a < b * b)         printf("Case %d: 0\n",i);       else       {           cnt=cal(a)/2;           for(int j=1;j<b;j++)           if(a%j==0)             cnt--;           printf("Case %d: %d\n",i,cnt);       }    }   return 0;}


阅读全文
0 0
原创粉丝点击