Home>

### number of python combinations

``````while True:
n, x = map (int, input (). split ())
if n == x == 0:
break
cnt = 0
for a in range (1, x // 3):
for b in range (a + 1, x // 2):
c = x --a --b
if b<c<= n:
cnt + = 1
print (cnt)``````

The above code is the code that came out by googled the solution of the problem of AIZU ONLINE JUDGE.

``````Number of combinations
From the numbers from 1 to n, select 3 numbers without duplication and create a program to find the number of combinations whose sum is x.
For example, a combination of 3 numbers from 1 to 5 that add up to 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are two ways.
Input
Multiple datasets are given as input. For each dataset, whitespace-separated n, x are given on one line.
When both n and x are 0, the input ends.
Constraints
3 ≤ n ≤ 100
0 ≤ x ≤ 300
Output
For each dataset, output the number of combinations on one line.``````

I think it's very compact and amazing, but I don't understand why cnt is incremented when "b I would appreciate it if you could tell me.

• Answer # 1

I don't understand why cnt is incremented when "b

`a`The value of`1`From "Total value`x`While changing to a number smaller than one-third of

`b`The value of`a + 1`From "Total value`x`While changing to a number less than half of

Obtained by calculation (x --a --b)`c`The value of`b`Larger and`n`Is

Then the total is`x`Combination to be`a, b, c`Because you have found a pair.

Supplement:

From the numbers from 1 to n, select 3 numbers without duplication and the number of combinations whose sum is x

Choosing three numbers from integers from 1 to n (not explicitly stated in the question sentence, but sure from the following sentences and examples) without duplication

Since each of the three integers is different, a magnitude relationship holds.

about it. So, in the blog that presents the solution,

Each of the three unique integers`a`,`b`,`c`To

For convenience,`aSuppose that the relationship of`

``` It is said. In other words, the smallest of the three integersa, The second smallest oneb, The third smallestcTreat as. ThenaIs "at least the total valuexIt is less than one-third of. " BecauseaAnd bigger than thatbOrcAnd the totalxBecause it has to be (if you allow duplication, the total valuexOne-third ofaIs the upper limit of). bThe value ofaMust be greater than the value ofxFromaIt also needs to be less than half the number minus the value of. BecausebAnd bigger than thatcThe total with and is "total valuexFromaThis is because it must be "the number obtained by subtracting the value of" (if duplication is allowed, the total valuexFromaHalf of the number minus the value ofbIs the upper limit of). cThe value of is "total valuexFromaWith the value ofbIt is calculated by "the number obtained by subtracting the value of".bNeed to be larger, yetnMust be: To search for the above efficientlyrangeSpecify a range of integers with, and when the condition is met (initial value 0)cntIncrease by 1 to get the final number of combinations. cnt + = 1Indented the same amount on the line immediately afterprint (f "{a} {b} {c}")If you try adding, the combinations found will be displayed, which will give you a better understanding. (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
• ``` Answer # 2 The point is that the three numbers are a for the smallest number, b for the next number, and c for the largest number. Then, if a and b are changed, c is sought and it is checked whether it is within n. When a and b are decided, c = x-a-b and c are decided. If c is greater than b and less than or equal to n, it is valid. Since a is the smallest number, adding the remaining b and c will increase it by at least 3 times. Checking up to 1/3 of x is sufficient. If b is less and c larger than b is added, it will be doubled, so checking up to 1/2 of x is sufficient. Separately, you can check a from 1 to x and b from a + 1 to x, but for the above reason, if a, there is nothing that exceeds 1/3. (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
``` ```
``` Related articlespython 3x - how to count the number of data extracted by pandas or how to solve the errorpython 3x - i want to output the name corresponding to the number entered in the excel filewhere is the minor version number of python that starts by default specified?python - when you want to judge by the number of characters from the back with a regular expressionpython - how to get the number of searches for a specific word within the periodpython - sumy by specifying the maximum number of characters with sumypython - i want to add a line number to the data framepython - i want to divide a binary number into two, store it in a list, and represent it in a different decimal numberpython - how to accept number input even if keypressevent is defined for qlineedit in pysidei want to invert the number 12345 in python like 54321 and display itget the number of characters from the python text boxget the total number and name of python groupinghow to align the number of elements with overlaplengt of python etcset python excel serial number to date and timehow to multiply each element by a number in a python 2d listhow to write to count the number of specific elements of dictionary values ​​in python3python - valueerror: field'id' expected a number but got'suzukitadashi'python - does the number of arguments match?python - how to count the number of characters in a character string including a regular expression ```
``` ```
``` Trends (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
``` (function() { if((".artleft pre").length > 0){ \$(".artleft pre").addClass('prettyprint linenums'); prettyPrint(); }; \$(".artleft p > code").each(function() { if (\$(this).text().length >= 30) { \$(this).addClass('linecode'); }else{} }); })(); ```
``` ```
``` © 2019 Programming tutorial for beginners ```