Arranging Cup-cakes

Our Chef is catering for a big corporate office party and is busy preparing different mouth watering dishes. The host has insisted that he serves his delicious cupcakes for dessert.

On the day of the party, the Chef was over-seeing all the food arrangements as well, ensuring that every item was in its designated position. The host was satisfied with everything except the cupcakes. He noticed they were arranged neatly in the shape of a
rectangle. He asks the Chef to make it as square-like as possible.

The Chef is in no mood to waste his cupcakes by transforming it into a perfect square arrangement. Instead, to fool the host, he asks you to arrange the N cupcakes as a rectangle so that the differencebetween the length and
the width is minimized.

Input

The first line of the input file contains an integer T, the number of test cases. Each of the following T lines contains a single integer N denoting the number of cupcakes.

Output

Output T lines, each indicating the minimum possible difference between the length and the width in a rectangular arrangement of the cupcakes.

Constraints

1 ≤ T ≤ 100

1 ≤ N ≤ 108

Example

  1. Input:
  2. 4
  3. 20
  4. 13
  5. 8
  6. 4
  7.  
  8. Output:
  9. 1
  10. 12
  11. 2
  12. 0

一题简单的因子分解题目。

思路有两个:

1 从根号2開始分解,第一个能够分解的两个因子就是答案

2 利用素数,找出全部的因子,然后求解

这里使用第二中方法。这个比較有挑战性。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class ArrangingCupcakes
  7. {
  8. const static int MAX_V = 10001;
  9. vector<int> sieve;
  10. void genSieve()
  11. {
  12. bool A[MAX_V] = {false};
  13. for (int i = 2; i < MAX_V; i++)
  14. {
  15. if (!A[i])
  16. {
  17. for (int j = (i<<1); j < MAX_V; j+=i)
  18. {
  19. A[j] = true;
  20. }
  21. sieve.push_back(i);
  22. }
  23. }
  24. }
  25. public:
  26. ArrangingCupcakes() : sieve()
  27. {
  28. genSieve();
  29. int T = 0, N = 0;
  30. scanf("%d", &T);
  31. while (T--)
  32. {
  33. scanf("%d", &N);//写成sanf("%d", N)就会程序崩溃
  34. vector<int> divs(1, 1);
  35. int n = N;
  36. for (int i = 0; i < (int)sieve.size() && n > 1; i++)
  37. {
  38. int sq = sieve[i];
  39. if (sq * sq > n) sq = n;
  40. if (n % sq == 0)
  41. {
  42. int degree = 0;
  43. for ( ; n % sq == 0; degree++) n /= sq;
  44.  
  45. for (int j = int(divs.size()) - 1; j >= 0 ; j--)
  46. {
  47. int d = divs[j];
  48. for (int k = 0; k < degree; k++)
  49. {
  50. d *= sq;
  51. divs.push_back(d);
  52. }
  53. }
  54. }//if (n % sq == 0)
  55. }//求因子分解
  56. int ans = N - 1;
  57. for (int i = 0; i < (int)divs.size(); i++)
  58. {
  59. ans = min(ans, abs(N/divs[i] - divs[i]));
  60. }
  61. printf("%d\n", ans);
  62. }//while (T--)
  63. }
  64. };

codechef Arranging Cup-cakes题解的更多相关文章

  1. Codechef Not a Triangle题解

    找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...

  2. CodeChef March Challenge 2019题解

    传送门 \(CHNUM\) 显然正数一组,负数一组 for(int T=read();T;--T){ n=read(),c=d=0; fp(i,1,n)x=read(),x>0?++c:++d; ...

  3. codechef MAY18 div2 部分题解

    T1 https://www.codechef.com/MAY18B/problems/RD19 刚开始zz了,其实很简单. 删除一个数不会使gcd变小,于是就只有0/1两种情况 T2 https:/ ...

  4. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  5. CodeChef April Challenge 2019题解

    传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...

  6. CodeChef August Lunchtime 2014 题解

    A题 给一个由a和b两种类型的字符组成的字符串,每次可以从中选取任意长度的回文子序列(不一定连续)并删除.问最少需要几次能将整个字符串为空. 思路:如果本身是个回文串,那么只需要一次,否则需要两次(第 ...

  7. CodeChef CBAL

    题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...

  8. CodeChef FNCS

    题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...

  9. Cup(二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2289 hdu_2289:Cup Time Limit: 3000/1000 MS (Java/Othe ...

随机推荐

  1. 【LeetCode 238】Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  2. Swift相关图书推荐

    Swift与Cocoa框架开发 作      者 [澳] 曼宁(Jonathon Manning),巴特菲尔德-艾迪生(Paris Buttfield 出 版 社 人民邮电出版社 出版时间 2015- ...

  3. 使用RSA非对称密钥算法实现硬件设备授权

    一.硬件设备授权 即用户在硬件设备输入一个序列号(或一个包含授权信息的文件),然后硬件设备便可正常使用.    二.授权方案 构思授权方案时,参考了下面网址的思路: http://bbs.csdn.n ...

  4. poj 3006 Dirichlet's Theorem on Arithmetic Progressions

    题目大意:a和d是两个互质的数,则序列a,a+d,a+2d,a+3d,a+4d ...... a+nd 中有无穷多个素数,给出a和d,找出序列中的第n个素数 #include <cstdio&g ...

  5. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  6. asp web api 怎么使用put和delete。

    Method Overriding RESTful services allow the clients to act on the resources through methods such as ...

  7. Android将ScrollView移动到最底部

    转载地址:http://hi.baidu.com/gaogaf/item/36e8a4c8ac6ba31050505848 scrollTo方法可以调整view的显示位置.在需要的地方调用以下方法即可 ...

  8. 【转】17种常用的JS正则表达式 非负浮点数 非负正数.

    <input type='text' id='SYS_PAGE_JumpPage' name='SYS_PAGE_JumpPage' size='3' maxlength='5' onkeyup ...

  9. 第二百一十八天 how can I 坚持

    真的是自以为是吗?或许是我想太多. 今天下雪了,2015年入冬以来的第一场雪,好冷. 又是一周. 睡觉吧,明天老贾生日. 没啥了,中午有点肚子疼,冬天了要注意.

  10. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...