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

Input:
4
20
13
8
4 Output:
1
12
2
0

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

思路有两个:

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

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

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

#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std; class ArrangingCupcakes
{
const static int MAX_V = 10001;
vector<int> sieve;
void genSieve()
{
bool A[MAX_V] = {false};
for (int i = 2; i < MAX_V; i++)
{
if (!A[i])
{
for (int j = (i<<1); j < MAX_V; j+=i)
{
A[j] = true;
}
sieve.push_back(i);
}
}
}
public:
ArrangingCupcakes() : sieve()
{
genSieve();
int T = 0, N = 0;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);//写成sanf("%d", N)就会程序崩溃
vector<int> divs(1, 1);
int n = N;
for (int i = 0; i < (int)sieve.size() && n > 1; i++)
{
int sq = sieve[i];
if (sq * sq > n) sq = n;
if (n % sq == 0)
{
int degree = 0;
for ( ; n % sq == 0; degree++) n /= sq; for (int j = int(divs.size()) - 1; j >= 0 ; j--)
{
int d = divs[j];
for (int k = 0; k < degree; k++)
{
d *= sq;
divs.push_back(d);
}
}
}//if (n % sq == 0)
}//求因子分解
int ans = N - 1;
for (int i = 0; i < (int)divs.size(); i++)
{
ans = min(ans, abs(N/divs[i] - divs[i]));
}
printf("%d\n", ans);
}//while (T--)
}
};

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. cocos2dx lua中继承与覆盖C++方法

    cocos2dx的extern.lua中的class方法为lua扩展了面向对象的功能,这使我们在开发中可以方便的继承原生类 但是用function返回对象的方法来继承C++类是没有super字段的,这 ...

  2. php-LAMP试题

    ylbtech-doc:php-LAMP试题 LAMP试题 1.A,LAMP试题返回顶部 1.{PHP LAMP题目}变量$email的值是字符串 user@example.com ,以下哪项能把字符 ...

  3. 如何配置Drupal数据库信息?

    Drupal的数据库连接信息通过文件settings.php中的变量$databases设置.变量$databases是一个二维的数组,第一维称为key,第二维称为target.使用这种方式可以处理多 ...

  4. duilib让不同的容器使用不同的滚动条样式

    装载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42240569 以前在给一个容器设置横纵向的滚动条时,一直是通过设置xml ...

  5. MXML的一些基本语法

    以下内容是一个视频的学习笔记<Flex4视频教程>,所以,先关记录也是以现在的Flash Builder为基础. <fx:Script/>  是脚本文件的声明 var代表数值, ...

  6. APIO2014 爆零总结

    真心爆零 不要不服 这次apio给了一种新的赛制 看上去很好? 所有人都可以在线提交 并且实时知道自己的分数 它对每个题目分成若干分数段 每个分数段有若干数据 要获得这个分数段的分数需要通过这个分数段 ...

  7. INTEL XDK 真机调试

    需要安装 1.google服务框架 2.google play 3.app preview

  8. EF6 在原有数据库中使用 CodeFirst 总复习(五、生成发帖页面)

    有点与在原有数据库中使用 CodeFirst 远了,不过是总复习吗,总得全面点. 一.在用户表(Users)中插入两个用户 二.生成发帖界面 MVC生成的界面很多,也没使用Ajax,实际开发中很少会使 ...

  9. 第二百零六天 how can I 坚持

    今天爬了趟香山,第三次去了,要征服北京这大大小小的山. 要征服三山五岳,然后...罗娜.哈哈. 爬了趟山好累,人好多. 我的铜钱草. 洗刷睡觉,还是明天给鱼换水吧,好懒.

  10. 转】MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3544208.html 感谢! 一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制 ...