CF #266 (Div. 2) B

来源:互联网 发布:网络法制知识竞赛答题 编辑:程序博客网 时间:2024/05/06 14:53
B. Wonder Room
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
题目链接:http://codeforces.com/contest/466/problem/B

The start of the new academic year brought about the problem of accommodation students into dormitories. One of such dormitories has a a × b square meter wonder room. The caretaker wants to accommodate exactly n students there. But the law says that there must be at least 6 square meters per student in a room (that is, the room for n students must have the area of at least 6n square meters). The caretaker can enlarge any (possibly both) side of the room by an arbitrary positive integer of meters. Help him change the room so as all n students could live in it and the total area of the room was as small as possible.

Input
The first line contains three space-separated integers n, a and b (1 ≤ n, a, b ≤ 109) — the number of students and the sizes of the room.

Output
Print three integers s, a1 and b1 (a ≤ a1; b ≤ b1) — the final area of the room and its sizes. If there are multiple optimal solutions, print any of them.

Sample test(s)
input

3 3 5
output
18
3 6
input
2 4 4
output
16

4 4



解题思路:

        题目大意就是让你求a * b 的面积覆盖n * 6的面积,求最小的覆盖面积,输出时要求a <= a1 , b <= b1.

首先第一个坑······n ,a , b的范围······刚一个a * b 就有可能超过int······所以要开成__int64.

  如果刚开始a * b的面积就大于n * 6,那么直接输出a * b 的面积和a、b的长度就好了;

否则的话分两种情况考虑。

当a <= b时,对a进行从a到sqrt(n * 6)的遍历,每次遍历求b' 时考虑奇偶数两种情况,当此时所求S大于n * 6  && 小于MIN && b' >= b时,进行更新;

同理当a > b 时,对b进行从b到sqrt(n * 6)的遍历,每次遍历求a' 时考虑奇偶两种情况,当此时所求S大于n * 6 && 小于MIN && a' >= a 时,进行更新; 


完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef __int64 LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    LL n , a , b;    while(~scanf("%I64d%I64d%I64d",&n,&a,&b))    {        if(a * b >= n * 6)        {            printf("%I64d\n%I64d %I64d\n" , a * b , a , b);            continue;        }        n = n * 6;        LL x , y , MIN = INFF , s , j;        if(a <= b)        {            for(LL i = a ; i <= sqrt(n) ; i++)            {                if(n % i == 0)                {                    j = n / i;                    s = j * i;                    if(s >= n && s <= MIN && j >= b)                    {                        MIN = s;                        x = i;                        y = j;                    }                }                else                {                    j = n / i + 1;                    s = i * j;                    if(s >= n && s <= MIN && j >= b)                    {                        MIN = s;                        x = i;                        y = j;                    }                }            }        }        else        {            for(LL i = b ; i <= sqrt(n) ; i ++)            {                if(n % i == 0)                {                    j = n / i;                    s = i * j;                    if(s >= n && s <= MIN && j >= a)                    {                        MIN = s;                        y = i;                        x = j;                    }                }                else                {                    j = n / i + 1;                    s = i * j;                    if(s >= n && s <= MIN && j >= a)                    {                        MIN = s;                        y = i;                        x = j;                    }                }            }        }        printf("%I64d\n%I64d %I64d\n" , MIN , x , y);    }}



0 0