51Nod 1305

传送门:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305

emmmmmmm太天真了,不会有水到直接按题意写的题的,zz了

(a[i]+a[j])/(a[i]*a[j])简单的拆分一下→1/a[i]+1/a[j];

floor向下取整,当a[i]为1时,与任意一个数都可以贡献1个1,也就是每个a[i]=1,贡献1*(n-1),a[i]为2时,只有a[j]也为2才会贡献一个1,假设共有cnt个2,那么贡献就是1*k*(k-1)/2;

#include<iostream>   
#include<algorithm>
#include<vector> 
#include<string.h>
using namespace std;
const int MAX=1e5+5;
int a[MAX];
int n;
int main()
{
    cin>>n;
    long long ans=0,cnt=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==1)
        ans+=(n-i);
        if(a[i]==2)
        cnt++;
    }
    ans+=cnt*(cnt-1)/2;
    cout<<ans<<endl;
     return 0;
 } 
原文地址:https://www.cnblogs.com/Egoist-/p/7618001.html