The method of hdu1722 bjfu1258 by dividing

Source: Internet
Author: User

The problem is a formula, the code is very simple. But I think there are not many people who really understand the principle. A lot of people just casually online search, find formula A, in fact, this to their own almost no improvement.

In view of the online about the problem of the problem-solving report almost no explanation of the principle, I will say a few more words, is not a strict proof, to share a bit.

The topic is that there are p people or Q people eat cakes, need to cut the cake in advance and can meet both cases, so that the minimum number of cake pieces. For the convenience of presentation, you may wish to set P < Q

First of all, the least common multiple of P and Q is M, then the average cake cut into m block, it must be able to meet the conditions, but this is not the optimal solution, tentatively as a solution ①.

Our job is to combine some of the M-Blocks of the solution ① (that is, not cutting them before), so that the number of blocks is as small as possible. So how do we merge?

The first to be sure, each block is 1/q, otherwise when the number of people is Q will not be. and the smallest of them is 1/m. So the solution ② can do this: first cut out P parts 1/q size of the block, the remainder of the whole cut into 1/m size, so it is clearly able to meet the requirements.

For example P = 2, q = 3, then cut into 1/3, 1/3, 1/6, 1/6. This example is exactly the optimal solution, but not always in all cases.

For example P = 6, q = 10, the best should be cut into 1/10, 1/10, 1/10, 1/10, 1/10, 1/10, 1/15, 1/15, 1/15, 1/15, 1/30, 1/30, 1/30, 1/30

The optimal solution for P = 60 should be 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60 , 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/60, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120, 1/120

Smart people should have seen, in fact, continue to 1/m the back of the small pieces of the merger, these small pieces are getting smaller, the last smallest are 1/m size. And how is the number of blocks in the middle size determined?

For example P = 6, q = 10 o'clock, the number of 1/15 blocks should be 4 instead of 6, because if the number is 6, only to meet the situation when the number is 6 (then each person eats a piece of 1/10, plus a 1/15), but when the number is 10 o'clock, these 1/15 of the small block can not form a large chunk of 1/10. Therefore, some smaller blocks must be reserved. At this point, 1/15 of the block as long as the reserved (q-p) = 4 blocks. The rest is recursion, that is, when you cut out 6 1/10 blocks, the next problem is equivalent to handling p = 10-6 = 4, q = 6, only then the remaining cake is just the original 4/10.

If p < Q/2 is established, for example, when p = 24, q = 60 o'clock, after cutting out the 1/60 blocks, you should continue to cut out 24 blocks of 1/60, and then consider the small pieces of 1/120.

This happens to be the process of seeking greatest common divisor by the method of dividing. The concrete principle everybody again savor.

Here's the code:

/** Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespacestd;intgcdintAintb) {intR;  while(b) {R= a%b; A=b; b=R; }    returnA;}intMain () {intp, q;  while(SCANF ("%d%d", &p, &q) = =2) {printf ("%d\n", p + Q-gcd (P, q)); }    return 0;}

The method of hdu1722 bjfu1258 by dividing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.