codechef Arranging Cup-cakes题解
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题解的更多相关文章
- Codechef Not a Triangle题解
找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...
- 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; ...
- codechef MAY18 div2 部分题解
T1 https://www.codechef.com/MAY18B/problems/RD19 刚开始zz了,其实很简单. 删除一个数不会使gcd变小,于是就只有0/1两种情况 T2 https:/ ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- CodeChef April Challenge 2019题解
传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...
- CodeChef August Lunchtime 2014 题解
A题 给一个由a和b两种类型的字符组成的字符串,每次可以从中选取任意长度的回文子序列(不一定连续)并删除.问最少需要几次能将整个字符串为空. 思路:如果本身是个回文串,那么只需要一次,否则需要两次(第 ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
- Cup(二分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2289 hdu_2289:Cup Time Limit: 3000/1000 MS (Java/Othe ...
随机推荐
- webtest 文章
一直也没有和游戏类测试打过交道,看到“腾讯WeTest ”提供的测试服务,以及和手机游戏相关的技术文章.在此作个备份记录的. 手游专题 http://wetest.qq.com/lab/tag/?ta ...
- 【LeetCode 239】Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 给编译好的DLL增加签名
两个步骤,记录如下,主要用在silverlight中引用的dll要签名时: "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm ...
- OPENGL学习笔记整理(五):着色语言
有些事情本身就是十分奇怪的.在传统上,图形硬件的设计目的是用于快速执行相同的硬编译指令集.不同的计算步骤可以被跳过,参数可以被调整,但计算本身确实固定不变的.然而,随着技术的发展,却越来越变得可以编程 ...
- C# 实现无焦点窗体(转载)
#region 无焦点窗体 [System.Runtime.InteropServices.DllImport("user32.dll")] private extern stat ...
- Emmet使用手册
语法: 1.后代:> 缩写:nav>ul>li < nav> < ul> < li></ li > ...
- SQLyog Enterprise 8.14
用户名:yunjian注册码:81f43d3dd20872b6 下载地址:SQLyog Enterprise 8.14
- fdquery update
fdquery update this->FDQuery1->CachedUpdates; this->FDQuery1->UpdateOptions->KeyFiel ...
- cocos2d-x 3.2 DrawNode 绘图API
关于Cocos2d-x 3.x 版本的绘图方法有两种: 1.使用DrawNode类绘制自定义图形. 2.继承Layer类重写draw()方法. 以上两种方法都可以绘制自定义图形,根据自己的需要选择合适 ...
- SQL Case When Then
适用sqlServer,oracle CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 T ...