The weight problem of French mathematician megziac

Source: Internet
Author: User
Tags ranges
Weight issue descriptionFrench mathematician metziac raised a question in his famous "Digital combo game" (1962): a merchant has a 40-pound weight, one day, I accidentally broke the weight into four parts. Then the merchant claimed that each piece of weight was a whole pound, and he found that the four fragments could be weighed between 1 and 40 lbs on an equal footing. What are the weights of these four fragments?

 

* Problem analysis and Algorithm Design DescriptionThis question is the development of the previous question. The condition given in the question is "On the Tianping", which means that the same weight can be placed on either the left side of the balance or the right side of the balance. If heavy objects can only be placed on the left side of the balance, when the balance is:
Weight + weight on the Left = weight on the right
Therefore, you can obtain the following information:
Weight = weight of the right weight-weight of the left
During programming, you only need to use the above formula to make "weight sum on the right-weight sum on the left" indicate all weights between 1 and 40. It should be noted in programming: how to use a simple method to indicate whether a weight is on the left of the balance, on the right of the balance, or is not used at all.
The following procedures use 1,-1, and 0 to indicate the above three situations, please understand. C code// * Program description and comment
# Include <stdio. h>
# Include <math. h>
Int main ()
{
Int weight1, weight2, weight3, weight4, D1, D2, D3, D4, X, flag;/* flag: match the meaning of the question */
Printf ("the weight is broke up as following 4 pieces :");
For (weight1 = 1; weight1 <= 40; weight1 ++)/* Splits 40 into four portions */
For (weight2 = weight1 + 1; weight2 <= 40-weight1; weight2 ++)
For (weight3 = weight2 + 1; weight3 <= 40-weight1-weight2; weight3 ++)
If (weight4 = 40-weight1-weight2-weight3)> = weight3)
{
For (flag = 1, x = 1; x <41 & flag; X ++)/* determines whether 1 ~ can be claimed ~ All weights between 40 */
For (flag = 0, d1 = 1; D1>-2; D1 --)/* place heavy objects on the left of the balance */
For (d2 = 1; D2>-2 &&! Flag; d2 --)/* 1: The weight is on the right of the balance */
For (D3 = 1; D3>-2 &&! Flag; D3 --)/* 0: this weight is not required */
For (D4 = 1; D4>-2 &&! Flag; D4 --)/*-1: The weight is on the left of the balance */
If (x = weight1 * D1 + weight2 * D2 + weight3 * D3 + weight4 * D4)
Flag = 1;
If (FLAG) printf ("% d/N", weight1, weight2, weight3, weight4 );
Flag = 0;
}
}

// * Running result
The weight is broke up as following 4 pieces: 1 3 9 27

  
Baidu explainedWe can calculate the four blocks, which are respectively 27,9, 3, and 1. because any one is 1-40. can be written as a * 3 ^ 3 + B * 3 ^ 2 + C * 3 ^ 1 + D * 3 ^ 0. construct a three-digit number. then, 1-40 is changed to three-digit number, which is (ABCD) 3.a, B, C, and D. The value range is {0, 1, 2 }. when the values of the four digits are 0 or 1, you can place the weights on the same disk. When the value of a digit is 2, put the weight of the previous digit and other required weights on a disk. if 33 lbs is needed, he uses a three-in number to represent (1020) 3, and puts 27 lbs and 9 lbs on the left disk, A 3-pound weight can be weighed by placing the right disk together with the heavy weight. in addition, if the value of the previous digit is 1 or 2, we should push another digit forward, for example, 15 lbs. The number of three-digit numbers is (0120 ), A 27-pound weight should be placed on the left, and a 9-pound and 3-pound weight should be placed on the right, and the weight can be weighed with heavy loads.
This uses the power of N3. the addition and subtraction operation can represent the principle of a positive integer. why? The number of the three-digit system just constructed can represent any positive integer. When performing the subtraction operation, it can be expressed as-1 on this digit. this means that his first digit should be added with 1, and the value of this digit is changed to 2. in this way, the number obtained after the addition and subtraction of the nth power of 3 can be expressed as a three-digit number, so it can represent any positive integer.

 

WriteTheorem: the number of M columns in ascending order {A (1), a (2 ),... A (m)}, set a (K) = Σ A (I), where I ranges from 1 to K, then
A (1) = 1 and A (J + 1) <= 2a (j) + 1, J is 1, 2 ,.., m-1 a series of weights that can be weighed {0, 1 ,.., A sufficient condition for any integer weight in the AM} range. In particular, when an equal sign is obtained above, this sequence is the only possible weight sequence with a (j) = 3 ^ (J-1) for J =,..., m

Inference: the sequence of objects whose weight is n to be divided into M parts and whose weight is an integer {A (1), a (2 ),.. A (m)}, Set M = Σ 3 ^ (I-1), where I from 1 to M, there are three situations:
1) m <n, no solution;
2) M = N, there is a unique solution a (j) = 3 ^ (J-1), j =,... m;
3) M> N, which may have multiple groups of solutions, is resolved to all integer sequences that meet (1) and Σ A (I) = n, where I ranges from 1 to M.

Proof of theorem:
(Adequacy)
Return the number:
When I = 1, A (I) = 1 is obviously true;
Assume that the theorem adequacy is true when I = K, that is, the weight W (k) that can be weighed by the first K weights that satisfy the formula (1) is 0 <= W (k) <= All integers of A (k). When I = k + 1, w (k + 1) can be weighed. The value must be 0 <= W (k + 1) <= All integers in the range of A (k + 1. The sections are discussed as follows:
(A) For 0 <= W (k + 1) <= a (K), it is clear that it can be weighed by the first K weights;
(B) For a (k) <W (k + 1) <= a (k + 1), assume 0 <= W (k) <= a (K ), exchange the weights of the left and right disks to generate the negative weight W (k) 'used with the weight a (k + 1). It can be-a (k) <= W (k) '<= All integers of 0. When used with the large weight a (k + 1), we can get a (k + 1) + W (k) ', which can certainly weigh all integers in a continuous range, because a (k + 1) <= 2a (k) + 1, A (k + 1)-A (k) <= a (k) + 1, therefore, the lower limit of a (k + 1) + W (k) 'is a (k + 1)-A (K), and the upper limit is a (k + 1 ), therefore, you can weigh all the W (k + 1) in a (k + 1) <W (k + 1 );
(C) For a (k + 1) <= W (k + 1) <= a (k + 1), similarly to (B), the upper and lower limits of weighing can be obtained as follows: A (k + 1) + a (K) = a (k + 1) and a (k + 1 );
Therefore, when I = k + 1, the theorem adequacy is also established, and the theorem adequacy is established by the regression method.

(Necessity)
When I = 1, it is clear that there must be a weight of 1 for the total;
I> 1, the opposite is true. If a K exists, (1) cannot be true, that is, 2a (k) + 1 <A (k + 1 ), then the weight a (k) + 1 neither can be weighed with the front of the K-1 weight, but because a (k + 1)-A (k)> A (k) + 1, but cannot be weighed with a (k + 1. Therefore, it is necessary to establish a conflict.

The inference can also be simply proved by the Return method. I will not prove it here, and I am too tired to type :)

Based on the above theorem and inference, it is easy to find a weight scheme that can be called out by M weights for any n-weight objects. When N = Σ 3 ^ (I-1), I from 1 to M, there is a unique solution a (I) = 3 ^ (I-1), can be rewritten to a (I) = 2 (Sigma AJ) + 1, where J from 1 to I-1, a cycle is directly output; when n> Sigma 3 ^ (I-1) When no solution; when n <Σ 3 ^ (I-1) as long as according to formula (1) and ensure that sigma A (I) = n search can be. Recursive search is supported. I will not write the program.

Http://www.zaoxue.com/article/tech-25044.htm

 

Method provided by csdn1 3 9 27: The back item is twice the sum of the preceding items plus 1
3 = 2x1 + 1
9 = 2x(1 + 3) + 1
27 = 2*(1 + 3 + 9) + 1

It is divided into several parts with that gold necklace to pay wages, which requires at least division and can satisfy various needs.

**************************************** ***************
It is a three-digit number. Any number is written as a three-digit number, and the equivalent value is converted to a three-digit representation above-1, 0, and 1,
Then we can know how to use these numbers to name them.

**************************************** ***************
// Weight DP Ideology
# Include <stdio. h>
Int main ()
{
Int num [6] = {1, 2, 3, 5, 10, 20}; // Weight Sequence
Int A [6]; // Number of 6 Weights
Bool vist [1000] = {false}; // access flag
Int no [1000]; // No [0] indicates the number of different weights. No [J] -- type J weight 1 <= j <= No [0]
Int I, J, K, total; // Total: The current weight.
For (I = 0; I <6; I ++)
Scanf ("% d", & A [I]); // enter the number of 6 Weights
No [0] = 1; no [1] = 0; // the first mass 0
For (I = 0; I <6; I ++) // stage: analyze the I weight
For (j = 0; j <no [0]; j ++) // status: enumeration of different existing Weights
For (k = 0; k <A [I]; k ++) // decision: based on the existing weight, place K block I weight
{
Total = No [J] + K * num [I]; // generate weight
If (! Vist [total])
{
Vist [total] = true; // if the weight has not been generated, set the access flag
No [0] ++;
No [No [0] = total;
// Printf ("% d/N", total );

}
}
Printf ("% d/N", no [0]-1 );
Return 0;
}

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.