HDU 5734 Acperience 【数学计算】

来源:互联网 发布:抗衰老精华 知乎 编辑:程序博客网 时间:2024/06/10 18:37

官方题解非常好,难得的良心题解





#include <iostream>#include <ctime>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cstring>#include <vector>#include <map>#include <string>using namespace std;typedef long long LL;const int maxn = 100000 + 100;int n;const LL inf = 1LL<<60;LL a[maxn];LL LLabs(LL a){if (a<0)return -a;return a;}inline LL gcd(LL a,LL b){LL r;while(b>0){r=a%b;a=b;b=r;}return a;}void init(){scanf("%d", &n);for (int i = 1; i<=n;++i)scanf("%lld", &a[i]);}void doit(){LL tmp1=0, tmp2=0;for (int i = 1;i<=n;++i)tmp1 += a[i] * a[i];for (int i = 1; i<=n;++i)tmp2 += LLabs(a[i]);tmp2 = tmp2 * tmp2;tmp1 = n * tmp1 - tmp2;LL tmp3 = gcd(tmp1, n);tmp1 /= tmp3;n/=tmp3;printf("%lld/%lld\n", tmp1, n);}int main(){int T;scanf("%d", &T);while (T--){init();doit();}return 0;}


0 0