Log inSign up
Problems / B112

Joining timbers

200 ptsCases 15Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

A woodshop has a stack of N timbers of different lengths. Every length is a natural number, and no two timbers have the same length.

A carpenter wants to pick two of these timbers and join them end to end into one post exactly X long. Joining them makes nothing shorter or longer. So the lengths of the two chosen timbers have to add up to exactly X.

The timbers are numbered 1 through N in the order they were stacked, and they are not sorted by length. The carpenter wants to count in advance how many ways the post can be made.

Write a program that takes the timber lengths and the post length X, and computes how many ways there are to pick two timbers whose lengths add up to exactly X. The order in which the two timbers are picked does not matter.

For example, suppose the lengths of N=9 timbers are given by the table below, and X=13.

Number123456789
Length512710912311

Finding every pair of timbers whose lengths add up to 13 gives the table below.

Pair123
Timbers picked2 and 64 and 87 and 9
Their lengths12 and 110 and 32 and 11
Total length131313

Go through the remaining timbers one by one. Timber 1 has length 5, so it needs a partner of length 8, and there is no timber of length 8. Timber 3 has length 7, so it needs a timber of length 6, and timber 5 has length 9, so it needs a timber of length 4. Neither exists. So the answer for this case is 3.

Since all the lengths are different, no timber is used in two pairs. The same timber also cannot be used twice.

Input and Output Examples

Example 1

Input

9
5 12 7 10 9 1 2 3 11
13

Output

3

Example 2

Input

3
759742 101870 196135
417007

Output

0

Example 3

Input

2
553357 164263
1782794

Output

0

Input

  • The first line holds the number of timbers N. N is a natural number from 1 to 100,000.

  • The second line holds the lengths of timbers 1 through N, separated by spaces, all on one line. Each length is a natural number from 1 to 1,000,000, and no two lengths are the same.

  • The third line holds the length of the post X. X is a natural number from 1 to 2,000,000.

  • The whole input is at most about 0.7MB. One line can hold up to 100,000 numbers, so read the input line by line and split each line on whitespace.

Output

  • Print the number of ways to pick two timbers whose lengths add up to exactly X on the first line. If there is no such pair, print 0.

Requirements

  • Every timber has a different length
  • The same timber cannot be picked twice
  • The order in which the two timbers are picked does not matter. Picking 2 and 6 counts as the same case as picking 6 and 2
  • The timber lengths come in the order given in the input, and they are not sorted
  • Do not count a pair whose total length is more or less than X. It has to be exactly X
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
2
553357 164263
1782794
0
3
759742 101870 196135
417007
0
9
5 12 7 10 9 1 2 3 11
13
3

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 55ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal2270ms216 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.