Description

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d =  . In the following, we assume that all lists have the same size n .

Input

The first line of the input file contains the size of the lists n (this value can be as large as ). We then have n lines containing four integer values (with absolute value as large as  ) that belong respectively to A, B, C and D .

Output

For each input file, your program has to write the number quadruplets whose sum is zero.

Sample Input

-   -
- -
- -
- - -
- -
- - -

Sample Output


Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-, -, , ), (, , -, -), (-, , , -),(-, , -, ), (-, -, , ).

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
#define ll long long
#define N 4006
int n;
int mp[N][N];
int A[N],B[N],C[N],D[N];
int CD[N*N];
void solve(){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
CD[i*n+j]=C[i]+D[j];
}
}
sort(CD,CD+n*n);
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
int cd = -(A[i]+B[j]);
ans+=upper_bound(CD,CD+n*n,cd)-lower_bound(CD,CD+n*n,cd);
}
}
printf("%I64d\n",ans);
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
for(int j=;j<;j++){
scanf("%d",&mp[i][j]);
}
}
for(int i=;i<n;i++){
A[i] = mp[i][];
B[i] = mp[i][];
C[i] = mp[i][];
D[i] = mp[i][];
}
solve();
}
return ;
}

最新文章

  1. 移动web基本知识
  2. Quartz2D 编程指南(四)位图与图像遮罩、CoreGraphics 绘制 Layer
  3. 详解Android中AsyncTask的使用
  4. 在SharePoint 2013中显示“以其他用户身份登录”
  5. Java-马士兵设计模式学习笔记-代理模式-动态代理 调用Proxy.newProxyInstance()
  6. HDU 1847 (博弈 找规律) Good Luck in CET-4 Everybody!
  7. AO创建IFeature的两种方法
  8. mybatis中为sql中传值#{}和${}的区别
  9. c# TCP/IP编程
  10. Ubuntu中PyCharm中字体设置
  11. WPF仿微软事件和属性窗体,效果更炫!
  12. asp.net mvc 实现记忆返回的功能
  13. HDOJ(HDU) 2143 box(简单的多次判断-用的卫条件)
  14. matrix矩阵求逆 与解方程模板 留做备用 (有bug,待补充)
  15. C++から広がり
  16. 响应的系统设置的事件——重写onConfigurationChanged响应系统设置更改
  17. Linux下一些命令
  18. MVC 前端页面ViewData参数名不区分大小写
  19. 微观:心流,宏观:ikigai
  20. Linux 基本使用

热门文章

  1. mac下Apache添加限速模块mod_bw
  2. 关于phpmyadmin中添加外键的做法
  3. 最好的Laravel中文文档
  4. Java 二维码生成工具类
  5. html5 canvas画进度条
  6. CSS背景特殊属性值
  7. Sql Server同步之订阅
  8. Segment对象
  9. 旧版asp.net 发送邮件代码
  10. js引用类型姿势