아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Same Sum Subsequences

시간 제한0.4초메모리 제한1024 MB

요약
길이 n이고 값이 [1,m]인 수열 A와 길이 m이고 값이 [1,n]인 수열 B가 주어질 때, 합이 같은 비어 있지 않은 부분수열을 각각 하나씩 출력한다.
난이도

보통10점 중 7점

유형
누적 합, 조합론, 수학
정답자
아직 제출이 없습니다

문제

You are given two positive integers n, m and two sequences of positive integers A and B. The sequence A consists of n elements, each one in the interval [1, m], while the sequence B consists of m elements, each one in the interval [1, n].

Write a program, which finds a nonempty subsequence of A and a nonempty subsequence of B, which have equal sums of elements.

Definition: For a sequence C = C0, C1, …, Cp, a subsequence of C is a sequence of elements Ci1, Ci2, …, Cik of C for which 0 ≤ i1 < i2 < ...< ik ≤ p.

입력

From the first line of the standard input, your program reads a positive integer n – the size of sequence A. From the second line, your program reads n positive integers – the elements of A. From the third line of the standard input, your program reads a positive integer m – the size of sequence B. From the fourth line, your program reads m positive integers – the elements of B.

출력

On the first line of the standard output, your program should print a positive integer p – the size of the chosen subsequence of A. On the second line, your program should print p integers – the indices of the chosen elements from A. On the third line of the standard output, your program should print a positive integer q – the size of the chosen subsequence of B. On the fourth line, your program should print q integers – the indices of the chosen elements from B.

Attention: Indices start from 0. The order in which your program prints the chosen indices doesn’t matter. It is guaranteed that at least one solution exists. If more than one solution exists, print any one of them.

제한

  • 1 ≤ n, m ≤ 1 000 000

힌트

a[1] + a[2] + a[4] = 3 + 3 + 3 = 9

b[0] + b[1] = 4 + 5 = 9.

Another possible solution is: a[2] +a[3] = 3 + 2 = 5 = b[1].

예제1

  1. 예제 1

    입력
    5
    2 3 3 2 3
    3
    4 5 5
    
    예상 출력
    3
    1 2 4
    2
    0 1