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 ...
随机推荐
- Java并发编程-关卡
CyclicBarrier 直译过来叫循环栅栏,它主要的方法就是一个:await().await() 方法没被调用一次,计数便会减少1,并阻塞住当前线程.当计数减至0时,阻塞解除,所有在此 Cycli ...
- bjfu1253 最大上升子序列和
n^2的算法就行,很简单的动态规划.直接上代码 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inclu ...
- js正则表达式中=s.g表示什么意思
//g是全局匹配//中间的内容表示:匹配以=开关,后面是0或多个空格,然后是双引号括起来的任意字符,比如:= "any symble" 匹配 = " asfjaskldf ...
- java中的类实现comparable接口 用于排序
import java.util.Arrays; public class SortApp { public static void main(String[] args) { Student[] s ...
- SQL Server 2008 备份改进版
1.Add compressing function with 7-Zip 2.With tool win.rar code so you can change it if you want USE ...
- Mysql的AB复制(主从复制)原理及实现
Mysql复制(replication)是一个异步的复制,从一个Mysql 实例(Master)复制到另一个Mysql 实例(Slave).实现整个主从复制,需要由Master服务器上的IO进程,和S ...
- Zookeeper,Hbase 伪分布,集群搭建
工作中一般使用的都是zookeeper和Hbase的分布式集群. more /etc/profile cd /usr/local zookeeper-3.4.5.tar.gz zookeeper在安装 ...
- openstack kilo manual arch 优化
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAALgCAIAAADwb7ujAAAgAElEQVR4nLy913Mj2Z7nx/vXKEIPG4
- Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)
Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...
- C# Common Keyword
[C# Common Keyword] 1.abstract Use the abstract modifier in a class declaration to indicate that a c ...