化簡-HDU-5734-Acperience
Acperience
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 236 Accepted Submission(s): 125
Problem Description
Deep neural networks (DNN) have shown significant improvements in several application domains including computer vision and speech recognition. In computer vision, a particular type of DNN, known as Convolutional Neural Networks (CNN), have demonstrated state-of-the-art results in object recognition and detection.
Convolutional neural networks show reliable results on object recognition and detection that are useful in real world applications. Concurrent to the recent progress in recognition, interesting advancements have been happening in virtual reality (VR by Oculus), augmented reality (AR by HoloLens), and smart wearable devices. Putting these two pieces together, we argue that it is the right time to equip smart portable devices with the power of state-of-the-art recognition systems. However, CNN-based recognition systems need large amounts of memory and computational power. While they perform well on expensive, GPU-based machines, they are often unsuitable for smaller devices like cell phones and embedded electronics.
In order to simplify the networks, Professor Zhang tries to introduce simple, efficient, and accurate approximations to CNNs by binarizing the weights. Professor Zhang needs your help.
More specifically, you are given a weighted vector W=(w1,w2,…,wn). Professor Zhang would like to find a binary vector B=(b1,b2,…,bn) (bi∈{+1,−1}) and a scaling factor α≥0 in such a manner that ∥W−αB∥^2 is minimum.
Note that ∥⋅∥ denotes the Euclidean norm (i.e. ∥X∥=sqrt(x1^2+…xn^2), where X=(x1,x2,…,xn)).
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integers n (1≤n≤100000) – the length of the vector. The next line contains n integers: w1,w2,…,wn (−10000≤wi≤10000).
Output
For each test case, output the minimum value of ∥W−αB∥^2 as an irreducible fraction “p/q” where p, q are integers, q>0.
Sample Input
3
4
1 2 3 4
4
2 2 2 2
5
5 6 2 3 4
Sample Output
5/1
0/1
10/1
Author
zimpha
Source
2016 Multi-University Training Contest 2
題意:
噼裡啪啦的一堆管理廢話,總之就是給定一個數組W,求∥W−αB∥^2的最小值,其中B是與W等大的、元素只為1或-1的陣列,α是任意實數。
題解:
先把題中式子展開來看,就是一個(x1−αB)^2+…(xn−αB)^2,因為B中元素是不定的,那麼再進一步展開,為了讓原式最小,成了(x1^2+…xn^2)−2α(abs(x1)+…abs(xn))+nα^2,(x1^2+…xn^2)是定值,那麼就是求−2α(x1+…xn)+nα^2這部分最小,讓s=(abs(x1)+…abs(xn)),這部分合併為nα^2-2sα,那麼很明顯n大於零,取得最小值點s/n,所以α的值就是s/n。將這個值帶入原式,成了(x1^2+…xn^2)-s*s/n。gcd一下後輸出就行了。
//
// main.cpp
// HDU-5734-Acperience
//
// Created by 袁子涵 on 16/7/21.
// Copyright © 2016年 袁子涵. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
using namespace::std;
int t;
long long int n,s,fa,tmp,fz,fm,yue;
long long int gcd(long long int a,long long int b)
{
if (b==0)
return a;
return gcd(b, a%b);
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false);
cin >> t;
while (t--) {
cin >> n;
s=fa=0;
for (long long int i=0; i<n ; i++) {
cin >> tmp;
fa+=tmp*tmp;
s+=abs(tmp);
}
fz=-s*s+n*fa;
fm=n;
yue=gcd(fz, fm);
cout << fz/yue << '/' << fm/yue << endl;
}
return 0;
}