LightOJ - 1341 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: 2ios

題意: 給出a和b,找出a的因子對,最小值不能小於b。c++

思路:根據算術基本原理(1)ide

(1)一個大於1的正整數N,若是它的標準分解式爲: ,那麼它的正因數個數爲  。
(2) 它的全體正因數之和爲  。
 時就稱N爲徹底數。 是否存在奇徹底數,是一個至今未解決之猜測。
(3) 利用算術基本定理能夠從新定義整數a和b的最大公因子  和最小公倍數  , 並證實  。
ui

因此能夠求出 a 的因子除以2,再減掉小於b的因子數,就是答案。spa

#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define maxn 1100000
#define mod 1000000007
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll tot;
bool vis[maxn+5];
int p[maxn+5];
void getp(){
    tot=0;
    for(ll i=2;i<=maxn;i++)vis[i]=true;
    for(ll i=2;i<=maxn;i++)if(vis[i]){
        p[tot++]=i;for(ll j=i*i;j<=maxn;j+=i)vis[j]=false;
    }
}
ll fac(ll n){
   if(n==0)return 0;
   ll ans=1;
   for(int i=0;i<tot;i++){
    if(n<p[i])break;
        if(n%p[i]==0){
            ll s=0;while(n%p[i]==0){s++;n/=p[i];};
            ans*=(s+1);
        }
   }
   if(n!=1)ans*=2;
   return ans;
}
int main(){
    getp();
    int t,test=0;
    scanf("%d",&t);
    while(t--){
        ll a,b,ans=0,c=0;
        scanf("%lld%lld",&a,&b);
        if(b>=sqrt(a*1.0))ans=0;
        else{
            for(ll i=1;i<b;i++)
                if(a%i==0)c++;
            ans=fac(a)/2-c;
        }
        printf("Case %d: %lld\n",++test,ans);
    }
}
相關文章
相關標籤/搜索