R1_G_Back and Forth

题面

G. Back and Forth

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Farmer John has two milking barns, each of which has a large milk tank as well as a storage closet containing 1010 buckets of various sizes. He likes to carry milk back and forth between the two barns as a means of exercise. On Monday, Farmer John measures exactly 10001000gallons of milk in the tank of the first barn, and exactly 10001000 gallons of milk in the tank of the second barn.

On Tuesday, he takes a bucket from the first barn, fills it, and carries the milk to the second barn, where he pours it into the storage tank. He leaves the bucket at the second barn.

On Wednesday, he takes a bucket from the second barn (possibly the one he left on Tuesday), fills it, and carries the milk to the first barn, where he pours it into the storage tank. He leaves the bucket at the first barn.

On Thursday, he takes a bucket from the first barn (possibly the one he left on Wednesday), fills it, and carries the milk to the second barn, where he pours it into the tank. He leaves the bucket at the second barn.

On Friday, he takes a bucket from the second barn (possibly the one he left on Tuesday or Thursday), fills it, and carries the milk to the first barn, where he pours it into the tank. He leaves the bucket at the first barn.

Farmer John then measures the milk in the tank of the first barn. How many possible different readings could he see?

Input

The first line of input contains 1010 integers, giving the sizes of the buckets initially at the first barn. The second line of input contains 1010more integers, giving the sizes of the buckets initially at the second barn. All bucket sizes are in the range 1…1001…100.

Output

Please print the number of possible readings Farmer John could get from measuring the milk in the tank of the first barn after Friday.

Example

input

1 1 1 1 1 1 1 1 1 2
5 5 5 5 5 5 5 5 5 5

output

5

Note

In this example, there are 5 possible results for the final amount of milk in the first barn’s tank:

  • 1000: FJ could carry the same bucket back and forth in each trip, leaving the total amount in the first barn’s tank unchanged.
  • 1003: FJ could carry 2 units on Tuesday, then 5 units on Wednesday, then 1 unit on Thursday, and 1 unit on Friday.
  • 1004: FJ could carry 1 unit on Tuesday, then 5 units on Wednesday, then 1 unit on Thursday, and 1 unit on Friday.
  • 1007: FJ could carry 1 unit on Tuesday, then 5 units on Wednesday, then 2 units on Thursday, and 5 units on Friday.
  • 1008: FJ could carry 1 unit on Tuesday, then 5 units on Wednesday, then 1 unit on Thursday, and 5 units on Friday.

题目大意

有A、B两个地点,这两个地点各有十个桶,每个桶都有一个大小。而且A、B两个地点各有一个装满1000升水的大缸。然后有个人在A、B两个地方往返两次。第一次从A到B,在A拿一个桶装满水到B,然后再在B拿一个桶(可以是从A拿来拿过来的那个)装满水回A。然后再执行一次上面的操作。问最后A的大缸里的水有多少种可能。

题目分析

一开始想得太多,同一个大小的桶归为一类什么的……其实分析一波复杂度也就是 1 0 4 10^4 次操作而已,直接模拟上就行了。

先对答案分个类,分为3类

  • 两次往返都是拿同一个桶
  • 有一次往返拿同一个桶
  • 从没有拿同一个桶

我们容易知道,第一种情况,大缸的水是不变的,只贡献了一种情况。

而有一次往返拿同一个桶,意味着只进行了一次往返,只是一个二重循环就行了

而第三种情况就是相当于一个四重循环,这里要注意不要重复拿同一个桶,第一次拿了一个桶去B,第二次去B的时候就不能拿那个桶去B了。

最后用一个集合来去重就行了(数组也行,我懒)。

代码

#include <cstdio>
#include <set>
using namespace std;
int a[10], b[10];
set<int> ans;
int main(int argc, char const *argv[]) {
  for(int i = 0; i < 10; i++)
    scanf("%d", &a[i]);
  for(int i = 0; i < 10; i++)
    scanf("%d", &b[i]);

  ans.insert(0);
  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 10; j++)
      ans.insert(a[i] - b[j]);

  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 10; j++)
      for(int k = 0; k < 10; k++)
        for(int l = 0; l < 10; l++)
          if(i != k && j != l)
            ans.insert(a[i] - b[j] + a[k] - b[l]);
  printf("%d\n", ans.size());
  return 0;
}

猜你喜欢

转载自blog.csdn.net/IT_w_TI/article/details/87830720