HDU5734 Acperience(数学推导)

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(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 (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

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;
}
时间: 2024-07-29 12:57:32

HDU5734 Acperience(数学推导)的相关文章

hdu-5734 Acperience(数学)

题目链接: Acperience Time Limit: 4000/2000 MS (Java/Others)   Memory Limit: 65536/65536 K (Java/Others) Problem Description Deep neural networks (DNN) have shown significant improvements in several application domains including computer vision and speech

HDU 5734 Acperience(数学推导)

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 (

HDU1719 Friend (数学推导)

friend numbers = 2^x + 3^y -1 1 #include<stdio.h> 2 int main() 3 { 4 __int64 a; 5 while(scanf("%I64d",&a)!=EOF) 6 { 7 if(!a) 8 { 9 printf("NO!\n"); 10 continue; 11 } 12 a+=1; 13 while(a%2==0||a%3==0) 14 { 15 if(a%2==0) a/=2;

HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 556    Accepted Submission(s): 127 Special Judge Problem Description Good news for us: to release the financial pressure, the government

leetcode 343. Integer Break(dp或数学推导)

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5

上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.pdf https://inst.eecs.berkeley.edu/~ee227a/fa10/login/l_dual_strong.html https://inst.eecs.berkeley.edu/~ee127a/book/login/l_sdual_slater.html http://w

HDU 5073 Galaxy(Anshan 2014)(数学推导,贪心)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 556    Accepted Submission(s): 127 Special Judge Problem Description Good news for us: to release the financial pressure, the government

最大熵模型中的数学推导

最大熵模型中的数学推导 查看原文,点击这里 0 引言 写完SVM之后,一直想继续写机器学习的系列,无奈一直时间不稳定且对各个模型算法的理解尚不够,所以导致迟迟未动笔.无独有偶,重写KMP得益于今年4月个人组织的算法班,而动笔继续写这个机器学习系列,正得益于今年10月组织的机器学习班. 10月26日机器学习班第6次课,身为讲师之一的邹博讲最大熵模型,他从熵的概念,讲到为何要最大熵.最大熵的推导,以及求解参数的IIS方法,整个过程讲得非常流畅,特别是其中的数学推导.晚上我把他的PPT 在微博上公开分

时域和频域变换之---傅里叶级数的数学推导

废话不多说先列提纲: 0.概述-需求分析-功能描述-受限和缺点改进+知识点预备 1.泰勒级数和傅里叶级数的本质区别,泰勒展开 2.  函数投影和向量正交 3.两个不变函数求导是本身e^x,sinx,cosx也是为什么要傅里叶转换的原因! 4.傅里叶技术推到过程 5.附录参考资料 0.有些时候,尤其是在图像处理中,矩阵运算数据量太大,特征提取量多,此时可以通过时域转频域来减少计算量,而且此转换不会损失数据完整性. 时域转频域的方法有周期函数用傅里叶技术,非周期函数(没有间断点的函数)用傅里叶转换,

Codeforces Round #360 (Div. 2) D 数学推导 E dp

Codeforces Round #360 (Div. 2) A  == B  水,但记一下: 第 n 个长度为偶数的回文数是  n+reverse(n). C    dfs 01染色,水 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i