As many people know, there are 88 keys on the piano. When a key is pressed, the frequency of the sound produced can be measured. As it turns out, a perfectly tuned piano has as Middle C the frequency of 440 Hz. From there, an octave above is exactly double this, or 880 Hz, and an octave below is exactly 220 Hz. As it turns out, an octave is actually defined mathematically as the note that is double or half of the starting note.
For the notes in between, they happen to be exactly evenly spaced based on the log scale! In fact, given the octave starting at Middle C, the first note to follow Middle C, or D, has a frequency of exactly $$$440 \cdot 2^{\frac{1}{12}}$$$, and the second note has a frequency of exactly $$$440 \cdot 2^{\frac{2}{12}}$$$, and so on and so forth. As it turns out, this pattern holds for all octaves.
Matt has developed a robot that listens to music, and after listening to Alice and Bob's performances of the same song, it has determined the central $$$n$$$ frequencies that make up the melody of both Alice and Bob's performances. Now, given that they performed on an infinitely long piano, and that they both performed for the same time, we can define the difference between the melodies as follows. For every note $$$i$$$, we take the note's difference as $$$b_i - a_i$$$, where $$$b_i$$$ is Bob's note's position on the piano, and $$$a_i$$$ is Alice's note's position on the piano. Taking the sum over all $$$i$$$, we can define the overall difference as the absolute value of that sum.
Given Alice and Bob's melodies in terms of frequencies, determine their difference!
The first line of the input will contain $$$n (1 \le n \le 10^4)$$$, the number of notes that both Alice and Bob will play.
The next $$$n$$$ lines of input will each contain two frequencies $$$f_i^a, f_i^b$$$, with the first being Alice's note, and the second being Bob's note that he plays at the same time. It is given that $$$10 \le f_i^a, f_i^b \le 10^{12}$$$. It is guaranteed each frequency will correspond to an exact note on an infinite piano within an error bound of $$$10^{-6}$$$.
A single integer, the overall difference between Alice and Bob's performances.
7 440.000000 493.883301 440.000000 493.883301 659.255114 739.988845 659.255114 739.988845 739.988845 830.609395 739.988845 830.609395 659.255114 739.988845
14
The sample input is Alice playing the first 7 notes of Twinkle Twinkle Little Star in C Major, and Bob playing the same piece in D major. There is a 2 note difference between every note, and hence the overall difference is 14.
Note: The introduction explaining musical frequencies is the same as part 1 of this problem.
As many people know, there are 88 keys on the piano. When a key is pressed, the frequency of the sound produced can be measured. As it turns out, a perfectly tuned piano has as Middle C the frequency of 440 Hz. From there, an octave above is exactly double this, or 880 Hz, and an octave below is exactly 220 Hz. As it turns out, an octave is actually defined mathematically as the note that is double or half of the starting note.
For the notes in between, they happen to be exactly evenly spaced based on the log scale! In fact, given the octave starting at Middle C, the first note to follow Middle C, or D, has a frequency of exactly $$$440 \cdot 2^{\frac{1}{12}}$$$, and the second note has a frequency of exactly $$$440 \cdot 2^{\frac{2}{12}}$$$, and so on and so forth. As it turns out, this pattern holds for all octaves.
Matt has developed a robot that listens to music, and after listening to Alice and Bob's performances of the same song, it has determined the central $$$n$$$ frequencies that make up the melody of both Alice and Bob's performances. Now, given that they performed on an infinitely long piano, and that they both performed for the same time, we can define the difference between the melodies as follows. For every note $$$i$$$, we take the note's difference as $$$b_i - a_i$$$, where $$$b_i$$$ is Bob's note's position on the piano, and $$$a_i$$$ is Alice's note's position on the piano. Taking the sum over all $$$i$$$, we can define the overall difference as the absolute value of that sum.
Given Alice and Bob's melodies in terms of frequencies, we already know how to determine their difference. However, Eve noticed that perhaps, Alice and Bob are playing in different scales! In other words, if we move all of Alice's notes up or down an integer amount on the piano, then the difference with Bob's performance can be reduced. Given this, determine with the best possible movement for Alice, the minimum difference between the two.
The first line of the input will contain $$$n (1 \le n \le 10^4)$$$, the number of notes that both Alice and Bob will play.
The next $$$n$$$ lines of input will each contain two frequencies $$$f_i^a, f_i^b$$$, with the first being Alice's note, and the second being Bob's note that he plays at the same time. It is given that $$$10 \le f_i^a, f_i^b \le 10^{12}$$$. It is guaranteed each frequency will correspond to an exact note on an infinite piano within an error bound of $$$10^{-6}$$$.
A single integer, the minimum possible overall difference between Alice and Bob's performances.
7 440.000000 493.883301 440.000000 493.883301 659.255114 739.988845 659.255114 739.988845 739.988845 830.609395 739.988845 830.609395 659.255114 739.988845
0
The sample input is Alice playing the first 7 notes of Twinkle Twinkle Little Star in C Major, and Bob playing the same piece in D major. Shifting Alice up by 2, there is no difference in their melodies, and hence the minimum possible overall difference is 0.
Algo the serial composer absolutely loves to write music and does it all the time. In fact, he has composed so many pieces over the years that now he has grown bored of having so much creative freedom and would like to give himself a challenge. Algo will now limit himself to composing with only the notes from a set of $$$N$$$ chosen note lengths, and he wants to match the overall target length of his song exactly.
For instance, if Algo wants to compose a 1-measure song using only quarter notes, half notes, and whole notes—of beat-lengths 1, 2, and 4 respectively—then there are 6 total possible songs that he could compose:
Algo has just been commissioned to write a song that is $$$M$$$ four-beat measures long. Determine how many unique songs ending at exactly $$$M$$$ measures Algo could possibly write, using only his $$$N$$$ chosen notes!
The first line of input contains two integers $$$M$$$ and $$$N$$$, where $$$M$$$ ($$$1 \leq M \leq 10^5$$$) is Algo's desired song length in 4-beat measures and $$$N$$$ ($$$1 \leq N \leq 20$$$) is the number of unique note lengths that Algo will limit himself to using. Then, the second line of input contains $$$N$$$ space-separated integers, where each integer $$$n_i$$$ ($$$1 \leq n_i \leq M*4$$$) represents a chosen note length in beats.
Output the total number of $$$M$$$-measure songs that Algo could possibly compose from his chosen notes! Because this amount could be very large, output the number of songs modulo $$$10^9+7$$$.
1 3 1 2 4
6
5 2 3 6
0
Ice-T has remained a powerful player in the rap game for almost forty years, ever since his debut in the early 80's. It is a little known secret that Ice-T powers his incredible rapping abilities by amassing points in a 2D plane and harvesting all of the 'T's that he can find among them.
A 'T' is defined to be a set of four points $$$A$$$, $$$B$$$, $$$C$$$, and $$$D$$$ where:
Help Ice-T become even more powerful! Given a set of $$$N$$$ points in 2D space, find the number of 'T's present.
The first line of input contains a single integer $$$N$$$ ($$$1 \leq N \leq 1000$$$), the number of points in the 2D plane that Ice-T has already amassed. $$$N$$$ lines follow, each containing two space-separated integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \leq x_i, y_i \leq 10^9$$$) which give the coordinate location of a unique point in the plane.
Output the total number of unique 'T's contained within the given points.
Note that a single point can contribute to multiple 'T' sets. Additionally, any orientation of a 'T' (vertical, horizontal, or upside-down) is acceptable to Ice-T, so include them all in your total.
11 5 10 7 6 7 4 7 2 1 8 3 8 1 6 3 6 2 4 2 6 5 8
3
As an avid musician, Santiago loves to practice his music and work on his fundamentals. One of his favorite exercises is playing through arbitrary scales with notes labelled from $$$1$$$ to $$$n$$$ in order. However, he's recently gotten tired of just playing the same scales over and over again and wants to switch up the order of the notes of the scale.
Before playing each note in the permuted scale, Santiago finds the highest previously played note lower than the current note, $$$l$$$, and the lowest previously played note higher than the current note, $$$r$$$, and defines the goodness of the note as the number of notes in the interval $$$(l, r)$$$ (exclusive). If no such $$$l$$$ exists, Santiago takes all notes equal to or below the current note as part of the interval and if no such $$$r$$$ exists, Santiago takes all notes equal to or above the current note as part of the interval.
Santiago then defines the total goodness of the scale ordering to be the sum of the goodness of the notes played within the scale ordering. Santiago feels he gets higher quality practice from scales with higher goodness. Given a proposed scale ordering, output the total goodness of the corresponding scale so that Santiago knows which scale orderings are worth practicing.
The input will consist of a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) giving the total number of notes in Santiago's scale. The next line consists of permutation of size $$$n$$$, $$$a_i$$$ $$$(1 \leq a_i \leq n)$$$, which represents that the $$$i$$$th note in the scale ordering is $$$a_i$$$.
Output the total goodness of the scale ordering as defined above.
5 3 4 5 2 1
11
The first note played, $$$3$$$, has a goodness of $$$5$$$ as no other notes have been played so all are included in the interval. The second note played, $$$4$$$, has a goodness of $$$2$$$ from the interval $$$(3, 5]$$$. The third note played, $$$5$$$, has a goodness of $$$1$$$ from the interval $$$(4, 5]$$$. The fourth note played, $$$2$$$, has a goodness of $$$2$$$ from the interval $$$[1, 3)$$$. The fifth note played, $$$1$$$, has a goodness of $$$1$$$ from the interval $$$[1, 2)$$$. Summing the goodness of each note gives us a total goodness of $$$5 + 2 + 1 + 2 + 1 = 11$$$.
Salma is looking to create her new masterpiece symphony. She has created $$$10^{5}$$$ unique small pieces, each piece assigned a unique number between $$$1$$$ and $$$10^{5}$$$ respectively, to potentially use within the symphony (each piece takes the same amount of time to play). During one round of editing, she picked a non-empty subset of those pieces of size $$$n$$$ ($$$1 \leq n \leq 10^{5}$$$) that she wants to use in the symphony.
Now, given those $$$n$$$ pieces, she realized that the combination of those pieces, aka melodic sequence, that tends to sound the best follows the following rule:
Salma would like to make her symphony as long as possible so she would like given the $$$n$$$ pieces that she has picked, help her find the length of the longest melodic sequence (satisfying the conditions mentioned above) that she can make.
The input has two lines. The first line contains a single integer $$$n$$$ where $$$1 \leq n \leq 10^{5}$$$ and $$$n$$$ represents the number of pieces that Salma wants to pick from.
The second line of input will contain $$$n$$$ space separated integers $$$p_1, p_2, \dots, p_n$$$ where $$$1 \leq p_i \leq 10^{5}$$$, $$$p_{i} \neq p_{j}$$$ for all $$$i \neq j$$$, and $$$p_{i-1} \lt p_{i}$$$ for $$$2 \leq i \leq n$$$ (the integers will be unique and given in strictly ascending order).
Output a single integer representing the number of pieces in the longest melodic sequence you can make from the input.
5 2 3 4 7 9
2
9 1 2 3 5 6 7 8 9 10
4
In the first example, the longest melodic sequence is either [2, 4] or [6, 9] both of which have length 2. In the second example, some example melodic sequences are [2, 3, 6, 9] or [2, 6, 8] or [2, 6, 8, 10], and the longest one is of length 4.
Yomo "Creati" Maoyorozu, the Everything Hero, is known for her "quirk" to create objects at will by using the amino acids stored up in her body in tandem with her vast knowledge of chemical compounds. As part of her training, Yomo would like to try to create a never-before-seen object by chaining some subset of $$$N$$$ available amino acids into a protein (for the sake of simplicity, assume the order of chaining acids does not matter in this problem).
Each amino acid has a unique ID that is a positive integer, but Yomo realizes that there is a restriction on certain subsets of amino acids being present in a chain. Her chemistry textbook states that having two acids with IDs that sum to a prime number makes a protein extremely prone to denaturation; thus Yomo would like to refrain from using certain amino acids to make her new object so that no two acids present in synthesis will have IDs that sum to a prime number.
Yomo wants your help to find the largest possible number of amino acids that can still be chained together without the resulting protein being susceptible to denaturation.
The first line of input contains a single integer $$$N$$$ ($$$1 \leq N \leq 2000$$$) representing the number of amino acids. The second line of input contains $$$N$$$ space-separated integers $$$r_i$$$ ($$$1 \leq r_i \leq 10^5$$$) indicating the IDs of the amino acids available. All IDs are distinct.
Output a single integer representing the largest number of amino acids that can be chained together such that none of the pairwise sums of their IDs are prime.
4 2 3 5 7
3
6 10 11 12 13 14 15
3
Mreddie Fercury is the vocalist for the locally famous Queen cover band, and he has been invited to perform his namesake's biggest hits in Moscow. It is quite a far distance from his hometown of Austin, which means Mreddie must travel via a series of flights. Although Mreddie does not particularly enjoy flying, he gets the most overanxious while waiting in airports. Every time he has a layover of $$$t$$$ minutes, his anxiousness increases by $$$t^2$$$ units.
We will define a layover as a contiguous period of time between the arrival of a flight and the departure of the next flight Mreddie boards, or the start of his journey (considered to be minute $$$0$$$) and the first flight Mreddie departs on.
Our protagonist is currently at airport $$$1$$$ in Austin and wants to travel to airport $$$N$$$ in Moscow using some series of the $$$M$$$ flights available to him. Since the total amount of time taken to travel to Moscow does not matter to Mreddie, he will even take flights that go back to the airport he started in to avoid time spent on the ground!
You, Mreddie's travel agent, need to find a sequence of flights that Mreddie can take to make it to Moscow while minimizing the units of anxiousness he accumulates while traveling. Good luck!
The first line of input contains two space-separated integers $$$N$$$ and $$$M$$$ ($$$1 \leq N, M \leq 200000$$$) representing the number of airports and the number of available flights, respectively. Each of the next $$$M$$$ lines contains four space-separated integers $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$ ($$$1 \leq a, b \leq N$$$, $$$0 \leq c \leq d \leq 10^9$$$) representing the departure airport, arrival airport, departure time (in minutes), and arrival time (in minutes), respectively.
All ordered pairs of flights have different departure times and different arrival times, and the departure time of one flight will be different from the arrival time of the other. For every flight, at least one of $$$a \neq b$$$ and $$$c \neq d$$$ will be true.
Output the minimum possible anxiousness level Mreddie will have to deal with if an optimal sequence of flights is chosen. If there is no sequence of flights that Mreddie can take to get from Austin to Moscow, then output $$$-1$$$.
4 5 1 3 40 50 3 3 70 100 3 4 110 1337 1 2 20 20 2 4 300 420
2100