http://lightoj.com/volume_showproblem.php?problem=1341

Aladdin and the Flying Carpet

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

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

题目大意:给出矩形面积ab,和组成该矩形的边的最小值,问这种面积为ab的矩形有几种

比如样例12 2,矩形面积为12,组成这样矩形的最小边为2,共有2种这样的矩形(2, 6),(3, 4)(这些边都大于或等于2,其中(2,6)和(6,2)是同一种)

这道题用到了唯一分解定理:N = p1^a1*p2^a2*p3^a3* ... *pn^an(其中p1、p2、... pn为N的因子,a1、a2、... 、an分别为因子的指数)

N的因子个数     M = (1 + a1)*(1 + a2)*(1 + a3)*...*(1 + an);

用唯一分解定理求出ab的因子个数,但题要求的是满足条件的因子对数,所以最终所求的因子个数需要除以2,然后再将不满足的减去

该题要用到筛选素数来缩短时间(减少循环次数)来防止TLE

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10. const int N = 1e6 + ;
  11.  
  12. int prime[N], k;
  13. bool Isprime[N];
  14.  
  15. void Prime()
  16. {
  17. k = ;
  18. memset(Isprime, true, sizeof(Isprime));
  19. Isprime[] = false;
  20. for(int i = ; i < N ; i++)
  21. {
  22. if(Isprime[i])
  23. {
  24. prime[k++] = i;
  25. for(int j = ; i * j < N ; j++)
  26. Isprime[i * j] = false;
  27. }
  28. }
  29. }//素数筛选
  30.  
  31. ll solve(ll n)
  32. {
  33. ll ans = , sum = ;
  34. for(ll i = ; i < k && prime[i] * prime[i] <= n ; i++)
  35. {
  36. if(n % prime[i] == )
  37. {
  38. ans = ;
  39. while(n % prime[i] == )
  40. {
  41. ans++;
  42. n /= prime[i];
  43. }
  44. sum *= ( + ans);
  45. }
  46. }
  47. if(n > )
  48. sum *= ;
  49. return sum;
  50. }//找n的因子个数
  51.  
  52. int main()
  53. {
  54. Prime();
  55. int t, x = ;
  56. ll ab, a, num;
  57. scanf("%d", &t);
  58. while(t--)
  59. {
  60. x++;
  61. scanf("%lld%lld", &ab, &a);
  62. if(ab < a * a)
  63. {
  64. printf("Case %d: 0\n", x);
  65. continue;
  66. }
  67. num = solve(ab);
  68. num /= ;
  69. for(ll i = ; i < a ; i++)
  70. if(ab % i == )
  71. num--;//将边小于a的情况减去
  72. printf("Case %d: %lld\n", x, num);
  73. }
  74. return ;
  75. }
 

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)的更多相关文章

  1. LightOJ - 1341 Aladdin and the Flying Carpet 唯一分解定理LightOJ 1220Mysterious Bacteria

    题意: ttt 组数据,第一个给定飞毯的面积为 sss,第二个是毯子的最短的边的长度大于等于这个数,毯子是矩形但不是正方形. 思路: 求出 sss 的所有因子,因为不可能是矩形,所以可以除以 222, ...

  2. LightOJ1341 Aladdin and the Flying Carpet —— 唯一分解定理

    题目链接:https://vjudge.net/problem/LightOJ-1341 1341 - Aladdin and the Flying Carpet    PDF (English) S ...

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

    http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...

  4. LightOJ 1341 Aladdin and the Flying Carpet 数学

    题意:给个矩形的面积a,和矩形的最小边长b,问有多少种矩形的方案(不能是正方形) 分析:a可以写成x,y,因为不能是正方形,所以设x<y,那么x<sqrt(a),y>sqrt(a) ...

  5. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  6. [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))

    题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...

  7. LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

    http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...

  8. LightOJ 1341 - Aladdin and the Flying Carpet

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...

  9. LightOJ 1341 Aladdin and the Flying Carpet【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...

随机推荐

  1. HDu 3449 (有依赖的01背包) Consumer

    题意: 有n件物品,对应有不同的价格和价值,这是典型的01背包.但现在有了一个限制,要买物品先买能装这件物品的特定的盒子,盒子的价值为0 代码理解得还不是太好,感觉这是一个“二重”的01背包.首先假设 ...

  2. 51nod1052 最大M子段和

    dp优化我总是不太熟练.这一次首先我写了O(n4)->O(n3)->O(n2).一步步的优化过来.yyl好像用的是单调队列优化dp我看不懂他的代码... O(n4) #include< ...

  3. bzoj2982: combination

    借(cao)鉴(xi)自popoqqq大爷的lucas定理的写法 #include<cstdio> #include<cstring> #include<cctype&g ...

  4. Codeforces 377 A Maze【DFS】

    题意:给出n*m的矩阵,矩阵由'.'和'#'组成,再给出k,表示需要在'.'处加k堵墙,使得剩下的'.'仍然是连通的 先统计出这个矩阵里面总的点数'.'为sum 因为题目说了一定会有一个解,所以找到一 ...

  5. appserver配置虚拟主机

    1, apach配置文件开启虚拟主机服务(C:\AppServ\Apache2.2\conf\httpd.conf)大概561行的位置 # Virtual hostsInclude conf/extr ...

  6. QCon 2015 阅读笔记 - 其他精选主题

    QCon 2015阅读笔记 QCon 2015 阅读笔记 - 移动开发最佳实践 QCon 2015 阅读笔记 - 团队建设 QCon 2015 阅读笔记 - 其他精选主题 以前分享过两个主题:移动开发 ...

  7. scala学习笔记(5)

    偏应用函数 举个例子 def sum(a: Int, b: Int, c: Int) = a + b + c val a = sum _ println(a(1,2,3)) 实际发生的事情是这样的:名 ...

  8. 正则表达式 java版

    众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学习及使用正则表达式,便成了解决这一矛 ...

  9. 【Django】Python虚拟环境工具virtualenv

    教程 第一步:安装virtualenv $pip install virtualenv 第二步:开启虚拟环境的python $cd ENV/Scripts $activate.bat #启用virtu ...

  10. 解决:cc1.exe: sorry, unimplemented: 64-bit mode not compiled in

    在win下用Go语言的cgo时(比如下面场景)我们会用到的GCC编译器,Win下我们一般用MinGW. Golang连接Oracle数据库:win下 golang 跨平台编译 MinGW全称Minim ...