uva 1152 4 values whose sum is zero ——yhx
来源:互联网 发布:医疗软件项目 编辑:程序博客网 时间:2024/05/29 19:26
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) 2 AB C D are such that a+b+c+d = 0. In the following, we
assume that all lists have the same size n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
The rst line of the input le contains the size of the lists n (this value can be as large as 4000).
We then have n lines containing four integer values (with absolute value as large as 228) that belong
respectively to A;B;C and D.
Output
For each test case, your program has to write the number quadruplets whose sum is zero.
The outputs of two consecutive cases will be separated by a blank line.
how many quadruplet (a; b; c; d) 2 AB C D are such that a+b+c+d = 0. In the following, we
assume that all lists have the same size n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
The rst line of the input le contains the size of the lists n (this value can be as large as 4000).
We then have n lines containing four integer values (with absolute value as large as 228) that belong
respectively to A;B;C and D.
Output
For each test case, your program has to write the number quadruplets whose sum is zero.
The outputs of two consecutive cases will be separated by a blank line.
1 #include<cstdio> 2 #include<cstring> 3 int abs(int x) 4 { 5 if (x>=0) return x; 6 return -x; 7 } 8 const int m=1098469; 9 int a[4010],b[4010],c[4010],d[4010],first[1100000],next[17000000],num[17000000];10 int main()11 {12 int i,j,k,n,p,q,x,y,z,t,ans;13 scanf("%d",&t);14 while (t--)15 {16 memset(a,0,sizeof(a));17 memset(b,0,sizeof(b));18 memset(c,0,sizeof(c));19 memset(d,0,sizeof(d));20 memset(first,0,sizeof(first));21 memset(next,0,sizeof(next));22 memset(num,0,sizeof(num));23 ans=0;24 scanf("%d",&n);25 for (i=1;i<=n;i++)26 scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);27 for (i=1;i<=n;i++)28 for (j=1;j<=n;j++)29 {30 x=a[i]+b[j];31 p=abs(x%m);32 next[(i-1)*n+j]=first[p];33 first[p]=(i-1)*n+j;34 num[(i-1)*n+j]=x;35 }36 for (i=1;i<=n;i++)37 for (j=1;j<=n;j++)38 {39 x=-c[i]-d[j];40 p=abs(x%m);41 for (k=first[p];k;k=next[k])42 if (x==num[k]) ans++;43 }44 printf("%d\n",ans);45 if (t) printf("\n");46 }47 }
枚举a+b,把所有值存起来,然后枚举-c-d,在a+b中查找。
具体查找方法是哈希,除k取余法即可。
0 0
- uva 1152 4 values whose sum is zero ——yhx
- uva 1152 4 values whose sum is zero ——yhx
- uva 1152(4 values whose sum is zero)
- UVA 1152(p237)----4 Values Whose Sum is Zero
- uva 1152 ——4 Values whose Sum is 0
- 和为0的4个值(4 Values Whose sum is Zero UVa 1152)
- UVA 1152 4 Values Whose Sum is Zero(好用的hash标记)
- UVa 1152 4 Values whose Sum is 0——二分
- 4 Values whose Sum is 0 UVA
- 4 Values whose Sum is 0 UVA
- 4 Values whose Sum is 0 UVA
- uva 1152-4 Values whose Sum is 0
- UVA - 1152 4 Values whose Sum is 0 二分法
- UVa 1152 4Values whose Sum is 0
- UVa 1152 4 Values whose Sum is 0
- UVA 1152 4 Values whose Sum is 0 (hash)
- Uva - 1152 - 4 Values whose Sum is 0
- UVA 1152 4 Values whose Sum is 0
- 将varchar修改为clob的方法
- uva 120 stacks of flapjacks ——yhx
- 稀疏矩阵
- 二分查找(折半查找) -- 已排序序列的查找算法
- uva 1605 building for UN ——yhx
- uva 1152 4 values whose sum is zero ——yhx
- 用反射和注解,绑定控件的id和点击事件
- Github Android 开源库前100名简介
- PE 427
- linux下设置自动启动服务(程序)
- Remove Element
- 插入排序 优化
- NOIP2008普及组传球游戏(动态规划)——yhx
- ORACLE的TO_CHAR函数详解