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题解

时间: 2024-10-17 15:52:53

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

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi

codechef - Bytelandian gold coins 题解

In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to ma

codechef Ciel and Receipt题解

Tomya is a girl. She loves Chef Ciel very much. Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose total price is exactly p. The current menus of Ciel's restaurant are shown the following table. Name of Men

codechef The Lead Game 题解

The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient. The game consists of several rounds and in each round both players obtain a score, based on

codechef The Morning Commute 题解

The Morning Commute The Chef commutes to work every day using the city's underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the stati

codechef Jewels and Stones 题解

Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery accessories. She has been collecting stones since her childhood - now she has become really good with identifying which ones are fake and which ones are no

codechef Three Way Communications 题解

The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they

Codechef Not a Triangle题解

找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,但是时间效率就是O(n*n*n)了,很慢. 不过既然是组合问题就必定可以使用排序后处理的方法降低时间效率的. 这里降低时间效率的方法是: 选一个最大的数c,然后选两个小数a和b,其中a < b,如果符合条件,那么两个数中间的数必定都可以作为b符合条件a + b < c 这样可以把时间效率降到O(n*n). 这个规律也不好找啊.要

codechef Subtraction Game 1题解

Subtraction Game 1 Chef is playing a game on a sequence of N positive integers, say A1, A2, ... AN. The game is played as follows. If all the numbers are equal, the game ends. Otherwise Select two numbers which are unequal Subtract the smaller number

codechef Closing the Tweets 题解

Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father's laptop. They saw it for the first time but were already getting bored to see a bunch of sente