1. 程式人生 > >HDU5734 Acperience(數學推導)

HDU5734 Acperience(數學推導)

pac together vector oge n-2 sub script cpe stream

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?αB2 is minimum.

Note that ? denotes the Euclidean norm (i.e. X=sqrt(x12+?+xn2 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 (1n100000) -- the length of the vector. The next line contains n integers: w1,w2,...,wn (?10000wi10000).

Output For each test case, output the minimum value of W?αB2 as an irreducible fraction "p/q" where p, q
are integers, q>0.
Sample
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

題意:

  已知一種計算方式||X||=sqrt(x12+x22+...+xn2),現給出W的值,求||W-aB||的最小值,其中B只能取-1和1,a為縮放因子,其中a>=0。

思路:

  實際上求(W1-ab1)2+(W2-ab22+...+(Wn-abn2的最小值,將這個公式展開,經過化簡可以得到(Wi的平方和)+na2-2a(Wi絕對值的和)

  求出a的值,然後將其帶入即可,相當於求一元二次方程的最小值。即a=-2a/b。

  最後註意分子、分母分開計算,最後GCD求最大公因數。GCD用long long。

代碼:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
using namespace std;
long long  gcd(long long a,long long b)//一定記得用longlong
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[100010];
        long long sum1,sum;
        sum1=sum=0;//sum1為平方和,sum為和
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
            if(a[i]<0)//一定取正值,因為b為-1或者+1,為了使最後結果最小,sum1應該最大
                a[i]=-a[i];
            sum1+=a[i];
            sum+=(a[i]*a[i]);
        }
        /*接下來註意不能用2a/b 因為他不一定為整數,最後求得是分數,所以要將最後的公式化作分子分母形式*/
        long long nb=n*sum-sum1*sum1 ;
        long long x;
        if(nb<n)
            x=gcd(n,nb);
        else
            x=gcd(nb,n);
        cout<<nb/x<<"/"<<n/x<<endl;
    }
    return 0;
}

HDU5734 Acperience(數學推導)