LightOJ 1341 唯一分解定理

Aladdin and the Flying Carpet

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status Practice LightOJ 1341

Appoint description: 
System Crawler  (2016-07-08)

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

1.有多少个约数:

先分解质因数
因数的次数分别是4,2,1

所以约数的个数为(4+1)*(2+1)*(1+1)=5*3*2=30个

eg:

先分解质因数

720=24*32*51

因数的次数分别是4,2,1

所以约数的个数为(4+1)*(2+1)*(1+1)=5*3*2=30个

2.所有约数之和:

2004的约数之和为:1, 2, 3, 4, 6, 12, 167, 334, 501, 668, 1002 ,2004  = 4704

如何求一个数所有约数之和呢?

首先,应用算术基本定理,化简为素数方幂的乘积。

X = a1^k1 * a2^k2........an^kn

X的所有素数之和可用公式(1+a1 + a1^2...a1^k1) * (1+a2 + a2^2...a2^k2) * .....(1+an + an^2...an^kn)表示

如:

2004 = 2^2  * 3  *167

2004所有因子之和为(1  + 2 + 2^2) * (1 + 3) * ( 1 + 167) = 4704;

程序实现的时候,可利用等比数列快速求1 + a1 + a1^2 + .....a1^n;

思路:

求出它的每个质因子的个数,然后用公式求出它的约数个数。如果b * b > a,那么值一定为0,其余部分可以枚举b,删除。但是我觉得枚举应该会挂掉,

但是竟然没有挂。。

/*
 * Author:  sweat122
 * Created Time:  2016/7/11 14:53:29
 * File Name: main.cpp
 */
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = 1000050;
int notprime[MAXN],prime[MAXN],cnt;
ll a,b;
void init(){
    cnt = 0;
    memset(prime,0,sizeof(prime));
    memset(notprime,0,sizeof(notprime));
    for(int i = 2; i < MAXN - 50; i++){
        if(!notprime[i]){
            prime[cnt++] = i;
        }
        for(int j = 0; j < cnt && 1LL * prime[j] * i < MAXN - 50; j++){
            notprime[prime[j] * i] = 1;
            if(i % prime[j] == 0) break;
        }
    }
}
int main(){
    int t,Case = 0;
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&a,&b);
        ll ans = 1;
        ll x = a;
        for(int i = 0; i < cnt; i++){
            if(prime[i] > x)break;
            if(x % prime[i] == 0){
                int num = 0;
                while(x % prime[i] == 0){
                    num += 1;
                    x /= prime[i];
                }
                ans *= (num + 1);
            }
        }
        if(x > 1) ans *= (1 + 1);
        ans /= 2;
        if(b * b > a){
            printf("Case %d: %lld\n",++Case,0);
        } else{
            for(int i = 1; i < b; i++){
                if(a % i == 0) ans -= 1;
            }
            printf("Case %d: %lld\n",++Case,ans);
        }
    }
    return 0;
}
时间: 2024-08-08 10:07:05

LightOJ 1341 唯一分解定理的相关文章

lightoj 1220 唯一分解定理

#include<bits/stdc++.h> using namespace std; #define maxn 1000005 #define ll long long int v[maxn],prime[maxn],m; void init(int n){ memset(v,0,sizeof v); memset(prime,0,sizeof prime); m=0; for(int i=2;i<=n;i++){ if(v[i]==0){ v[i]=i; prime[++m]=i;

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路:根据唯一分解定理,把X写成若干素数相乘的形式,则X的正因数的个数为:(1+a1)(1+a2)(1+a3)...(1+an).(ai为指数) 因为这道题目是求矩形,所以知道一个正因数后,另一个正因数也就确定了,所以每组正因数重复计算了两遍,需要除以2. 最后减去小于b的因数. 1 #include<

1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 什么叫唯一分解定理:算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1a1P2a2P3a3......Pnan,这里P1<P2<P3......<Pn均为质数,其中指数ai是正整数.这样的分解称为 N 的标准分解式 我们求出n的因

lightoj 1236 正整数唯一分解定理

A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;   

HDU 1452 Happy 2004(唯一分解定理)

题目链接:传送门 题意: 求2004^x的所有约数的和. 分析: 由唯一分解定理可知 x=p1^a1*p2^a2*...*pn^an 那么其约数和 sum = (p1^0+p1^1^-+p1^a1)*-* (pn^0+pn^1^-+pn ) 代码如下: #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const

NOIP2009Hankson 的趣味题[唯一分解定理|暴力]

题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解了如何求两个正整数 c1 和 c2 的最大公约数和最小公倍数.现 在 Hankson 认为自己已经熟练地掌握了这些知识,他开始思考一个“求公约数”和“求公 倍数”之类问题的“逆问题”,这个问题是这样的:已知正整数 a0,a1,b0,b1,设某未知正整 数 x 满足: 1. x 和 a0 的最大公约

欧几里德算法和唯一分解定理

刘汝佳<入门经典>上提供了一道经典的题目: 除法表达式,在NYOJ上可以找到原题,题号1013 描述 给出一个这样的除法表达式:X1/X2/X3/···/Xk,其中Xi是正整数.除法表达式应当按照从左到右的顺序求和,例如表达式1/2/1/2值为1/4.但是可以在表达式中嵌入括号以改变计算顺序,例如表达式(1/2)/(1/2)的值为1. 输入 首先输入一个N,表示有N组测试数据, 每组数据输入占一行,为一个除法 表 达式,输入保证合法. 使表达式的值为整数.k<=10000,Xi<=

Coderforce-574C Bear and Poker(素数唯一分解定理)

题目大意:给出n个数,问能不能通过让所有的数都乘以2的任意幂或乘以3的任意幂,使这n个数全都相等. 题目分析:最终n个数都是相等的,假设那个数为x,根据素数唯一分解定理,x能分解成m*2p3q.所以,只需将所有的a[i]一直除以2并且一直除以3,最终只需判断这n个数是否全部相等即可. 代码如下: # include<iostream> # include<cstdio> # include<cmath> # include<string> # include