2020.09.13组队训练赛 问题 C: Cocoa Coalition (数学)

问题 C: Cocoa Coalition
时间限制: 1 Sec 内存限制: 128 MB

题目描述
Alice and Bob decide to share a chocolate bar, which is an n by m rectangular grid of chocolate cells. They decide that Alice should get a < n·m pieces and that Bob should get b = n·m − a pieces. To split the chocolate bar, they repeatedly take a single piece of chocolate and break it either horizontally or vertically, creating two smaller pieces of chocolate. See Figure C.1 for an
example.
What is the minimum number of splits that Alice and Bob need to perform in order to split the n-by-m chocolate bar into two piles consisting of a and b chocolate cells?

Figure C.1: Illustration of a solution to Sample Input 2, showing the original 10-by-10 chocolate bar split three times into pieces of size 10-by-2, 10-by-5, 3-by-3 and 7-by-3. Giving Alice the 10-by-5 and 7-by-3 pieces, she gets a total of 50 + 21 = 71 chocolate cells.
输入
The input consists of a single line, containing the three integers n, m and a (1 ≤ n, m ≤ 106 ,1 ≤ a < n · m).
输出
Output the minimum number of splits needed to achieve the desired division of chocolate.
样例输入 Copy
3 10 9
样例输出 Copy
1

题意:
给你一个矩形的巧克力,问你最少切几次可以分割出来一块面积为a的巧克力。

思路:
发现最多切三次。一般需要一次两次或三次。除去一次两次的就是三次。
具体过程看代码:

#include
using namespace std;
const int N=1e3+15;
typedef long long ll;
int main()
{
     
    ll n,m,a,b;
    cin>>n>>m>>a;
    if(n>m)
        swap(n,m);
    b=n*m-a;
    if(a%n==0||a%m==0)
    {
     
        cout<<"1"<<endl;    return 0;
    }
    for(int i=1;i<=sqrt(a);i++)
    {
     
        if(a%i==0)
        {
     
            ll l1=i,l2=a/i;
            if(l1>l2)
                swap(l1,l2);
            if(n>=l1&&m>=l2)
            {
     
                cout<<"2"<<endl;    return 0;
            }
        }
    }
    a=n*m-a;
    for(int i=1;i<=sqrt(a);i++)
    {
     
        if(a%i==0)
        {
     
            ll l1=i,l2=a/i;
            if(l1>l2)
                swap(l1,l2);
            if(n>=l1&&m>=l2)
            {
     
                cout<<"2"<<endl;    return 0;
            }
        }
    }
    cout<<"3"<<endl;
    return 0;
}

你可能感兴趣的:(联合训练赛)