File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+ static StringTokenizer st;
9+ static int T ;
10+ static int N ,M ;
11+ static long [][] dp;
12+ static long count;
13+
14+ public static void main (String [] args ) throws Exception {
15+ T = Integer . parseInt(br. readLine());
16+ N = Integer . parseInt(br. readLine());
17+ int [] A = new int [N ];
18+ st = new StringTokenizer (br. readLine());
19+ for (int i = 0 ; i < N ; i++ ) {
20+ A [i] = Integer . parseInt(st. nextToken());
21+ }
22+
23+ M = Integer . parseInt(br. readLine());
24+ int [] B = new int [M ];
25+ st = new StringTokenizer (br. readLine());
26+ for (int i = 0 ; i < M ; i++ ) {
27+ B [i] = Integer . parseInt(st. nextToken());
28+ }
29+
30+ dp = new long [N ][N ];
31+ Map<Long ,Integer > m = new HashMap<> ();
32+ for (int i = 0 ; i < N ; i++ ) {
33+ for (int j = 0 ; i+ j < N ; j++ ) {
34+ if (i == 0 ){
35+ dp[j][j] = A [j];
36+ }else {
37+ dp[j][i+ j] = dp[j][i+ j- 1 ] + A [i+ j];
38+ }
39+ long target = T - dp[j][i+ j];
40+ m. put(target, m. getOrDefault(target,0 )+ 1 );
41+ }
42+ }
43+
44+ count = 0 ;
45+ dp = new long [M ][M ];
46+ for (int i = 0 ; i < M ; i++ ) {
47+ for (int j = 0 ; i+ j < M ; j++ ) {
48+ if (i == 0 ){
49+ dp[j][j] = B [j];
50+ }else {
51+ dp[j][i+ j] = dp[j][i+ j- 1 ] + B [i+ j];
52+ }
53+ Integer target = m. get(dp[j][i+ j]);
54+ if (target != null ){
55+ count += target;
56+ }
57+ }
58+ }
59+
60+ bw. write(count + " " );
61+ bw. close();
62+ }
63+
64+ }
65+ ```
You can’t perform that action at this time.
0 commit comments