Table of Contents
Method to find solutions
C code for the above method
Example
Output
Explanation of the above code
Conclusion
Home Backend Development C++ Written in C++, find the number of triangles formed by a set of points on three lines

Written in C++, find the number of triangles formed by a set of points on three lines

Sep 09, 2023 am 09:53 AM
point written in c Wire number of triangles

Written in C++, find the number of triangles formed by a set of points on three lines

Now we have several points present in the 3 rows; for example, we need to find out how many triangles these points can form

1

2

3

4

5

Input: m = 3, n = 4, k = 5

Output: 205

 

Input: m = 2, n = 2, k = 1

Output: 10

Copy after login

We will apply some combinations Mathematics to solve this problem and formulate some formulas to solve this problem.

Method to find solutions

In this method we will devise a formula: applying combinatorics to the current situation, this formula will provide us with the result.

C code for the above method

This is the C syntax we can use as input to solve the given problem -

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#include <bits/stdc++.h>

 

#define MOD 1000000007

 

using namespace std;

 

long long fact(long long n) {

   if(n <= 1)

   return 1;

   return ((n % MOD) * (fact(n-1) % MOD)) % MOD;

}

long long comb(int n, int r) {

   return (((fact(n)) % MOD) / ((fact(r) % MOD) * (fact(n-r) % MOD)) % MOD);

}

 

int main() {

   int n = 3;

   int m = 4;

   int r = 5;

   long long linen = comb(n, 3); // the combination of n with 3.

   long long linem = comb(m, 3); // the combination of m with 3.

   long long liner = comb(r, 3); //the combination of r with 3.

   long long answer = comb(n + m + r, 3); // all possible comb of n, m , r with 3.

   answer -= (linen + linem + liner);

   cout << answer << "\n";

   return 0;

}

Copy after login

Output

1

205

Copy after login

Explanation of the above code

In this method, we find all possible combinations of n m r and three numbers, that is, comb(n m r, 3). Now, you know that the condition for three points to become a triangle is that they cannot be collinear, so we find all possible collinear points obtained by summing the combinations of n, m, r, and then combine this sum with n m r By subtracting the changes in the three numbers, we get the answer and print it out.

Conclusion

This article discusses how to calculate how many triangles can be formed from a set of points on three lines by applying combinatorics. We also learned the C program and complete method (normal method) to solve this problem. We can write the same program in other languages ​​like C, Java, Python and others. Hope this article is helpful to you.

The above is the detailed content of Written in C++, find the number of triangles formed by a set of points on three lines. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to type the point '丶' using the keyboard? How to type the point '丶' using the keyboard? Feb 15, 2024 pm 01:51 PM

When typing on a keyboard, many users are curious about how to type the point "丶" on the keyboard? So let’s take a look at the method that the editor brings to you on how to type this “丶” symbol on the keyboard. 1. Click "丶" and type directly on the keyboard [dian]. You will see the punctuation mark [丶] on the selection bar. 2. Special symbols In the Sogou Pinyin input method, when switching to Chinese mode, pressing the v key will cause some special symbols to appear. These symbols include numbers (eg: v123), dates (eg: v2013/1/1), calculations (eg: v1+1) and functions (eg: v2~3). These symbols make it easy to enter a variety of different information. 2. Then press the number key again, any number from 0 to 9 can be used

Written in C++, find the number of triangles formed by a set of points on three lines Written in C++, find the number of triangles formed by a set of points on three lines Sep 09, 2023 am 09:53 AM

Now we get several points present in 3 rows; for example, we need to find out how many triangles these points can form Input:m=3,n=4,k=5Output:205Input:m=2,n=2, k=1Output:10 We will apply some combinatorial mathematics to solve this problem and formulate some formulas to solve this problem. Method to find a solution In this method we will devise a formula: applying combinatorics to the current situation, this formula will provide us with the result. C++ code for the above method This is the C++ syntax we can use as input to solve the given problem - example #include<bits/stdc++.h>#define

How to break the line into equal parts in CAD How to break the line into equal parts in CAD Feb 27, 2024 am 11:40 AM

In the CAD design process, we often need to divide a certain line or curve into equal parts. This requirement is extremely common in a variety of scenarios, such as engineering drawing, product design, urban planning, etc. For example, the equal division function is particularly important when it is necessary to arrange lamp posts evenly within a specific distance, or to set screws equidistantly on the sides of a product. In order to meet this precise segmentation requirement, CAD software provides us with a variety of tools and methods. So how to interrupt the line? This tutorial guide will give you a detailed introduction. Users who want to know more about it can come and learn together below! Share the method of breaking the line into equal parts using CAD 1. Open the CAD2023 software and create CAD graphics. As shown below: 2. Click Modify

How to check if three points are collinear in Java? How to check if three points are collinear in Java? Sep 05, 2023 pm 06:41 PM

If three points lie on a straight line, they are said to be collinear. If the points are not on the same straight line, they are not collinear. This means that if three points (x1,y1), (x2,y2), (x3,y3) are on the same straight line, they are collinear. Among them, x1, y1, x2, y2, x3, y3 are points on the x-axis and y-axis, (x1, y1), (x2, y2), (x3, y3) are the coordinates. Mathematically, there are two ways to determine whether three points are collinear. Find the area of ​​a triangle by using the points. If the area of ​​the triangle is zero, then the three points are collinear. Formulatofindareaoftriangle=0.5*[x1*(y2-y3)+x2*

How to find the midpoint of a line in Java? How to find the midpoint of a line in Java? Sep 02, 2023 pm 06:45 PM

Suppose (x1, y1) is the starting point of the line and (x2, y2) is the end point of the line. To get the midpoint of a straight line we have to use the midpoint of a straight line formula. Midpoint=((x1+x2)/2,(y1+y2)/2) In this article, we will see how to find the midpoint of a line segment using Java programming language, when the two points of the line segment are known. Show you some examples Example 1 Suppose the two points are (2,3) and (3,5) By using the midpoint formula of the line segment, a=(x1+x2)/2=(2+3)/2= 2.5b=(y1+y2)/2=(3+5)/2=4.0 Therefore, the midpoint of the line is (2.5,4.0) Example 2 Suppose the two points are (2,-3) and (-3 ,5)

Programming in C++, find the number of paths from one point to another in a grid Programming in C++, find the number of paths from one point to another in a grid Aug 29, 2023 pm 10:25 PM

In this article, we are given a problem where we need to find the total number of paths from point A to point B, where A and B are fixed points, i.e. A is the upper left corner point in the grid and B is the Lower right corner point, for example −Input:N=5Output:252Input:N=4Output:70Input:N=3Output:20 In the given problem, we can formalize the answer and derive the result through simple observations. Method of finding the solution In this method we come up with a formula by observing that when crossing the grid from A to B we need to go right n times and down n times which means we need to find all possible path combinations, so we get

What is the number of pins in the Antec 650 power supply motherboard cable interface? (Antec 650 power supply wiring diagram) What is the number of pins in the Antec 650 power supply motherboard cable interface? (Antec 650 power supply wiring diagram) Jan 03, 2024 am 10:46 AM

How many pins does the Antec 650w motherboard cable have? The power cable of the Antec 650W power supply motherboard is usually 24 pins, which is the largest power interface on the motherboard. Its function is to connect the motherboard and power supply to provide power to the motherboard and other system components. In addition, the Antec 650W power supply may also include other types of power interfaces, such as CPU8pin, PCIe6+2pin, etc., for connecting other components such as the CPU and independent graphics cards. Motherboard routing tutorial Motherboard routing is the process of connecting circuits between various electronic components when designing a motherboard. In this process, factors such as circuit stability, signal transmission speed and accuracy need to be considered. When routing wiring according to the circuit diagram, pay attention to the layout and select appropriate line width and distance to avoid

Maximum number of distinct straight lines through a point in C Maximum number of distinct straight lines through a point in C Aug 26, 2023 am 10:25 AM

We get the number N for each line and the coordinates of the two points (x1,y1) and (x2,y2). The goal is to find the maximum number of straight lines from the given straight lines that can pass through a single point such that no two straight lines cover each other and no rotation is performed. We will represent the straight line as (pair)m,c) where y=mx+c and m is the slope m=y2-y1/x2-x1 Given c1!=c2, lines with the same m are parallel. We will calculate different slopes in meters. For a vertical line, if x1=x2, then slope=INT_MAX, otherwise m. Let us understand with an example. Input Line1(x1,y1)=(4,10)(x2,y2)=(2,2)Line2(x1,y1)=(2

See all articles