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 ...
随机推荐
- Delphi 异或,英文为exclusive OR,或缩写成xor
异或,英文为exclusive OR,或缩写成xor 异或(xor)是一个数学运算符.它应用于逻辑运算.异或的数学符号为“⊕”,计算机符号为“xor”.其运算法则为: a⊕b = (¬a ∧ b) ∨ ...
- MySQL中批量插入数据
不管怎么样, 你需要大量的数据, 那么问题来了, 怎么快速地插入呢? 1. 这是我创建的一个批量插入的存储过程… 当然, 你可以把参数去掉, 一次性插入1W, 10W… CREATE DEFINER= ...
- import Tkinter的时候报错
在看到图形界面编程的时候,需要导入Tkinter模块,从而在解释器中进行import Tkinter,然后...报错如下: >>> from tkinter import * Tra ...
- 一起刷LeetCode5-Longest Palindromic Substring
发现自己原来掌握的一下算法,都忘掉了,啊啊啊 ----------------------------------------------------------------------------- ...
- [转]32位和64位系统区别及int字节数
一)64位系统和32位有什么区别? 1.64bit CPU拥有更大的寻址能力,最大支持到16GB内存,而32bit只支持4G内存 2.64位CPU一次可提取64位数据,比32位提高了一倍,理论上性能会 ...
- 使用JavaMail API发送邮件
发送邮件是很常用的功能,注册验证,找回密码,到货通知,欠费提醒等,都可以通过邮件来提醒. Java中发送邮件需要使用javax.mail.jar包,读者可以上网搜索或去官方下载,下载地址为: 下面贴上 ...
- mysql 中时间和日期函数应用
一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now(); +-------------------- ...
- 如何开启php报错
今天碰到一个很二的问题,安装了php网站之后,发现nginx一直无法解析到index.php文件,显示为空白,从后台的日志来看是500错误,但是同目录下的phpinfo.php却可以正常解析.想来应该 ...
- 王家林的“云计算分布式大数据Hadoop实战高手之路---从零开始”的第十一讲Hadoop图文训练课程:MapReduce的原理机制和流程图剖析
这一讲我们主要剖析MapReduce的原理机制和流程. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发 ...
- 使用Sunny-grok实现内网转发
Sunny-grok 申请地址:http://www.ngrok.cc ngrok.cfg配置: server_addr: "server.ngrok.cc:4443" auth_ ...