规律-51nod-1305 Pairwise Sum and Divide

来源:互联网 发布:平板电脑mac地址查询 编辑:程序博客网 时间:2024/06/05 07:32

数学渣渣,头一次推出数学规律。。记下来庆祝下。抓狂

有这样一段程序,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。

思路:
题中给出是向下取整。那么如果 1 +1 /1*1 会产生2. 1 与其他数如此操作 会产生1.
2+2 /2*2 会产生1. 2与其他数如此操作 会产生0.
那么规律为:
1的个数a 个 2的个数 b个 3的个数c个
1与1可以产生 a*(a-1)/2个组合 和为a*(a-1)
1与其他数可以产生 a*(b+c)个组合 和为a*(b+c)
2与2可以产生 b*(b-1)/2个组合 和为b*(b-1)/2
ans=a*(a-1)+a*(b+c)+(b-1)*b/2
#include <iostream>#include <stdio.h>using namespace std;int a,b,c;int main(){    int n;    cin>>n;    while(n--)    {        int t;        cin>>t;        if(t==1)            a++;        else if(t==2)            b++;        else            c++;    }    cout<<a*(a-1)+a*(b+c)+(b-1)*b/2<<endl;    return 0;}


0 0