1. 程式人生 > >51nod P1305 Pairwise Sum and Divide ——思路題

51nod P1305 Pairwise Sum and Divide ——思路題

沒有 scan 找規律 floor iostream span 把他 F12 out

久しぶり!

發現的一道有意思的題,想了半天都沒有找到規律,結果竟然是思路題。。(在大佬題解的幫助下)

原題戳>>https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305<<

有這樣一段程序,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
一開始看到這個公式,直接腦補到了求並聯電路電阻和的公式,然後神TM在這上面找規律。。。然後gg
其實這題的數字中只有1和2是有作用的。從公式裏可以看出,兩者都大於2的所有組合在向下取整後都會為0
例如5 7 ——>5+7/5x7<1--->0
而2和2組合則是1 2 2——>2+2/2*2=1
1和其他如何數組合都是1
所以這題統計一下3個數據——1的數目、2的數目、其他數數目(總數減前兩者)即可。
之後再把他們按前面的結論組合求解即可,代碼如下:
#include<iostream>
#include
<stdio.h> using namespace std; int a[100005]; int main() { int n,i,j,e1=0,e2=0; cin>>n; for(i=0;i<n;i++) { scanf("%d",&a[i]); if(a[i]==1) e1++; if(a[i]==2) e2++; } long long ans=0; ans+=(e1-1)*(e1-1)+(e1-1); ans+=e1*(n-e1); ans+=((e2-1
)*(e2-1)+(e2-1))/2; cout<<ans<<endl; }





51nod P1305 Pairwise Sum and Divide ——思路題