码迷,mamicode.com
首页 > 其他好文 > 详细

Light OJ 1341 Aladdin and the Flying Carpet

时间:2015-02-16 18:23:03      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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

Output for Sample Input

2

10 2

12 2

Case 1: 1

Case 2: 2

 

唯一分解定律的应用:
求a的大于等于b的约数个数。先求出总的约数个数,然后剔除小于b的即可。
考虑每个素因数都有bi个,那么有(bi+1)种选择。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e6+5;
bool vis[maxn+10];
int prim[maxn/10];
LL a,b;
int t,cnt=0;
void init()
{
    int m=sqrt(maxn+0.5);
    for(int i=2;i*i<=maxn;i++)
        if(!vis[i])
          for(int j=i*i;j<=maxn;j+=i)  vis[j]=1;
    for(int i=2;i<=maxn;i++)
        if(!vis[i])  prim[cnt++]=i;
}
LL solve()
{
    LL ans=1,temp=a;
    if(a/b<b)  return 0;
    for(int i=0;i<cnt&&prim[i]*prim[i]<=a;i++)
    {
        int c=0;
        while(a%prim[i]==0)
        {
            c++;
            a/=prim[i];
        }
        ans*=(c+1);
    }
    if(a>1) ans<<=1;
    ans>>=1;//有重复
    for(int i=1;i<b;i++)
        if(temp%i==0)  ans--;
    return ans;
}
int main()
{
    int cas=1;
    init();
    cin>>t;
    while(t--)
    {
         cin>>a>>b;
         cout<<"Case "<<cas++<<": "<<solve()<<endl;
    }
    return 0;
}
Light OJ 1236:

Find the result of the following code:

long long pairsFormLCM( int n ) {
    long long res = 0;
    for( int i = 1; i <= n; i++ )
        for( int j = i; j <= n; j++ )
           if( lcm(i, j) == n ) res++; // lcm means least common multiple
    return res;
}

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

Output

For each case, print the case number and the value returned by the function ‘pairsFormLCM(n)‘.

Sample Input

Output for Sample Input

15

2

3

4

6

8

10

12

15

18

20

21

24

25

27

29

Case 1: 2

Case 2: 2

Case 3: 3

Case 4: 5

Case 5: 4

Case 6: 5

Case 7: 8

Case 8: 5

Case 9: 8

Case 10: 8

Case 11: 5

Case 12: 11

Case 13: 3

Case 14: 4

Case 15: 2

 

求LCM(i,j)(i<=j)为n的对数:求素数分解式,若bi是第i个素数的幂,那么对于这两个数
中有一个的幂一定是bi,另一个随意,那么对于第i的素数的分配方案有(2*bi+1)种
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e7+5;
bool vis[maxn+10];
int prim[maxn/10];
int cnt=0;
void init()
{
    for(int i=2;i*i<=maxn;i++)
        if(!vis[i])
        for(int j=i*i;j<=maxn;j+=i)  vis[j]=1;
    for(int i=2;i<=maxn;i++)
        if(!vis[i])  prim[cnt++]=i;
}
LL t,n;
int main()
{
    init();int cas=1;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        LL ans=1;
        for(int i=0;i<cnt&&prim[i]*prim[i]<=n;i++)
        {
            if(n%prim[i]==0)
            {
                int c=0;
                while(n%prim[i]==0)
                {
                    c++;
                    n/=prim[i];
                }
                ans*=(2*c+1);
            }
        }
        if(n>1) ans*=3;
        printf("Case %d: %d\n",cas++,(ans+1)/2);
    }
    return 0;
}



Light OJ 1341 Aladdin and the Flying Carpet

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/43853391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!