https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305&judgeId=579072
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
有这样一段程序,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
原以为按照公式。。。。。超时
(A[i]+A[j])/A[i]*A[j]=1/A[j]+1/A[i]
统计1和2的次数,1乘的次数为 如果1出现的次数为n;n(总次数-1);而已经计算过1结果现在只需要考虑2,因为1已经和2乘过了,现在只有A[i],A[j]同时为2时才会出现1 n(n-1)
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<map>
#include<math.h>
#define inf 0x3f3f3f
#define ll long long
using namespace std;
int main()
{
ll n,sum1=0,sum2=0,other=0,m;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>m;
if(m==1)
sum1++;
else if(m==2)
sum2++;
else
other++;
}
int ans;
ans=sum1*(sum1+sum2+other-1)+sum2*(sum2-1)/2;
cout<<ans<<endl;
return 0;
}