有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:
fun(A)
sum = 0
for i = 1 to A.length
for j = i+1 to A.length
sum = sum + Floor((A[i]+A[j])/(A[i]*A[j]))
return sum
给出数组A,由你来计算fun(A)的结果。例如:A = {1, 4, 1},fun(A) = [5/4] + [2/1] + [5/4] = 1 + 2 + 1 = 4。
Input
第1行:1个数N,表示数组A的长度(1 <= N <= 100000)。 第2 - N + 1行:每行1个数A[i](1 <= A[i] <= 10^9)。
Output
输出fun(A)的计算结果。
Input示例
3 1 4 1
Output示例
4
思路:
1、观察算式:floor(A+B)/(A*B),不难发现,如果其中A.B都是>=2的数值,那么对应的值一定是0.那么根据这个特性我们继续讨论:
①如果A=1&&B=1,那么值为2,
②如果A=1||B=1&&另外一个不是1,那么值为1,
③如果A=2&&B=2,那么值为1.
根据以上特性,我们肯定是要来判断a【i】==1||a【i】==2的个数来决定结果。
2、对于每一个1,我们考虑,其贡献出来的值为:n-1,那么我们一层for扫这个数组,如果有一个1,那么对应结果加上n-1.
接下来我们统计2的个数,对应在最终结果上加:(cont2-1+1)*(cont2-1)/2;
3、数据范围比较大, 注意使用LL.
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
int main()
{
int n;
while(~scanf("%d",&n))
{
ll cont2=0;
ll output=0;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
if(x==1)
{
output+=n-1;
}
if(x==2)
{
cont2++;
}
}
printf("%I64d\n",output+(cont2)*(cont2-1)/2);
}
}