LightOJ1341 Aladdin and the Flying Carpet —— 唯一分解定理
题目链接:https://vjudge.net/problem/LightOJ-1341
Time Limit: 3 second(s) | Memory Limit: 32 MB |
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 |
题意:
求n有多少对因子(即a*b=n,则{a,b}为其中一对,不要求顺序),且因子的最小下限为m?
题解:
1.求因子个数,则对n进行质因子分解。
2.先不考虑因子的最小下限m,则n共有 ∏(num[i]+1)个因子,其中num[i]为第i个质因子的个数。
3.当n是平方数的时候,除了sqrt(n)之外,其他因子是一一对应的,即a*b=n, a!=b; 当n不是平方数时,显然它的因子是一一对应的,即有2*pair个因子,所以有pair对因子, 因此 pair = (∏(num[i]+1))/2,而题目说明了n非平方数。
4.再考虑回下限m:可知当 a*b = n 时, a、b必定满足 a<=sqrt(n)或b<=sqrt(n)。所以当m>sqrt(n)时,就不存在这样的因子对。当m<=sqrt(n)时,可以直接枚举较小的那个因子,枚举范围为:1~min(m, sqrt(n)+1),如果它能整除n,这表明存在一对不满足最小下限的因子对,删除即可。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; LL n, m;
bool notprime[MAXN];
int prime[MAXN+];
void getPrime()
{
memset(notprime, false, sizeof(notprime));
notprime[] = notprime[] = true;
prime[] = ;
for (int i = ; i<=MAXN; i++)
{
if (!notprime[i])prime[++prime[]] = i;
for (int j = ; j<=prime[ ]&& prime[j]<=MAXN/i; j++)
{
notprime[prime[j]*i] = true;
if (i%prime[j] == ) break;
}
}
} int fatCnt;
LL factor[][];
int getFactors()
{
LL tmp = n;
fatCnt = ;
for(int i = ; prime[i]<=tmp/prime[i]; i++)
{
if(tmp%prime[i]==)
{
factor[++fatCnt][] = prime[i];
factor[fatCnt][] = ;
while(tmp%prime[i]==) tmp /= prime[i], factor[fatCnt][]++;
}
}
if(tmp>) factor[++fatCnt][] = tmp, factor[fatCnt][] = ;
} int main()
{
getPrime();
int T, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld", &n,&m);
int ans = ;
if(1LL*m*m>n)
ans = ;
else
{
getFactors();
for(int i = ; i<=fatCnt; i++)
ans *= (factor[i][]+);
ans /= ;
for(LL i = ; i<min(m,(LL)sqrt(n)+); i++)
if(n%i==) ans--;
}
printf("Case %d: %lld\n", ++kase, ans);
}
}
LightOJ1341 Aladdin and the Flying Carpet —— 唯一分解定理的更多相关文章
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- LightOJ - 1341 Aladdin and the Flying Carpet 唯一分解定理LightOJ 1220Mysterious Bacteria
题意: ttt 组数据,第一个给定飞毯的面积为 sss,第二个是毯子的最短的边的长度大于等于这个数,毯子是矩形但不是正方形. 思路: 求出 sss 的所有因子,因为不可能是矩形,所以可以除以 222, ...
- LightOJ-1341 Aladdin and the Flying Carpet 分解质因数(注意对大素数的优化)
题目链接:https://cn.vjudge.net/problem/LightOJ-1341 题意 给出一个长方形的面积a 让你算整数边长的可能取值,并且两个边都大于给定数字b 思路 唯一分解定理: ...
- LightOJ1341 Aladdin and the Flying Carpet
题意 给一对数字 a,b ,a是一个长方形的面积,问有多少种整数的边的组合可以组成面积为a的长方形,要求最短的边不得小于b 数据组数T<=4000, a,b<=10^12 Solution ...
- Aladdin and the Flying Carpet
Aladdin and the Flying Carpet https://cn.vjudge.net/contest/288520#problem/C It's said that Aladdin ...
- C - Aladdin and the Flying Carpet 有多少种长方形满足面积为a(<=10^12),且最短边>=b;长方形边长为整数,且一定不可以是正方形。
/** 题目:C - Aladdin and the Flying Carpet 链接:https://vjudge.net/contest/154246#problem/C 题意:有多少种长方形满足 ...
- Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】
Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...
- Aladdin and the Flying Carpet(唯一分解定理)
题目大意:给两个数a,b,求满足c*d==a且c>=b且d>=b的c,d二元组对数,(c,d)和(d,c)属于同一种情况: 题目分析:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个 ...
随机推荐
- 做IT这几年,我整理了这些干货想要送给你!
没有一条路是容易的,特别是转行计算机这条路. 松哥接触过很多转行做开发的小伙伴,我了解到很多转行人的不容易,记得松哥大二时刚刚决定转行计算机,完全不知道这些东西到底应该怎么学,每天就是抱着书啃,书倒是 ...
- Stockbroker Grapevine(最短路)
poj——1125 Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36112 ...
- java常用组件
一.Jpanel 1.面板:容器类组件 2.用途:与Layout配合使用,JFrame—>JPanel—>Layout 二.JTextField 1.文本框 2.JPasswordFiel ...
- 访问权限修饰符Protected专题
上图描述:A类在a包下,m()方法被protected修饰 上图描述:B类也在a包下,B类是A类的子类. 解析:B类和A类是同包类,B类是A类的子类,因此b对象可以调用m()方法. 上图描述:C类也在 ...
- String,StringBuffer,StringBuilder源码分析
1.类结构 String Diagrams StringBuffer Diagrams StringBuilder Diagrams 通过以上Diagrams可以看出,String,StringBuf ...
- spring lifeCycle
Lifecycle接口定义了每个对象的生命周期.如下 public interface Lifecycle { void start(); void stop(); boolean isRunning ...
- Mybatis Generator插件和PageHelper使用
最近,开始接触web项目开发,项目使用springboot和mybatis,以前一直以为开发过程中实体类,mybatis的xml文件都需要自己手动的去创建. 同事推荐说Mybatis Generato ...
- Java中Javadoc的{@link}与@see的简单区别
{@link}与@see这两个Javadoc注解都可以直接链接类和方法.用法基本一致. 但是@see必须顶头写,而{@link可以任意地方},如下所示: 参考: http://blog.csdn.ne ...
- cocos2d-x 事件分发机制 ——触摸事件监听
cocos2d-x 3.0 出来已经好久了,也已经用3.0写了几个小游戏,感觉3.0的事件触发机制太赞了,随这里总结一下.也算是对知识的一种回顾和加深理解. 3.0的事件分发机制中.须要也只须要通过创 ...
- 第六讲_图像分割Image Segmentation
第六讲_图像分割Image Segmentation 语义分割(semantic segmentation) 常用神经网络介绍对比-FCN SegNet U-net DeconvNet 目录 +三大数 ...