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的所有正约数的个 ...
随机推荐
- HDU 2460 Network 傻逼Tarjan
Network Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- JDBC_完整版
1,新建WEB项目:JDBC 2,导入驱动 将mysql-connector-java-5.0.8-bin.jar包放入web-inf目录下面的lib目录中 3,新建User类,放入entity包中 ...
- 关于查看python的trace的方法
lptrace本质上是基于GDB的,进入到进程内存空间,然后执行了一段python指令把当时的trace给print出来 使用工具:https://github.com/khamidou/lptrac ...
- 在Intellij上面导入项目 & AOP示例项目 & AspectJ学习 & Spring AoP学习
为了学习这篇文章里面下载的代码:http://www.cnblogs.com/charlesblc/p/6083687.html 需要用Intellij导入一个已有工程.源文件原始内容也可见:link ...
- mac 下 virtualbox 配置全网通
mac下virtualbox实现主机和虚拟机.虚拟机和外网互访的方案 全局添加Host-Only网络 Adapter IPv4 Address:192.168.56.1 IPv4 Network Ma ...
- Linux Distribution
来自为知笔记(Wiz)
- jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径
遇到的问题是这种,在jsp网页中嵌入了本地的图片,由于会用到上传到服务器的图片,所以没有放到项目里面,而是把全部图片单独放到一个文件夹里,然后打算使用绝对路径把要显示的图片显示出来.比方是放在了E盘的 ...
- Cts框架解析(15)-任务运行完
case运行完成后.会回到CtsTest的run方法中: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L ...
- tempdb 相关总结
/* -- 0. 高速压缩tempdb为初始值 USE tempdb DBCC SHRINKFILE(2,TRUNCATEONLY); */ -- 1. tempdb以下未回收的暂时表 ,某些版本号可 ...
- python(31)- 模块练习
1. 小程序:根据用户输入选择可以完成以下功能: 创意文件,如果路径不存在,创建文件夹后再创建文件 能够查看当前路径 在当前目录及其所有子目录下查找文件名包含指定字符串的文件 ...