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 (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∥=√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 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

题意

给你一个n维向量w,求∥W?αB∥2的最小值,其中B=(b1,b2,...,bn) (bi∈{+1,?1}),α≥0

题解

开始误以为是平均数最小,WA了几次后开始推式子

min(∥w?αb∥2)=min(∑(wi2-2αbiwi2bi2))

由于bi∈{+1,?1},易得bi*w≥0

=min(∑(wi2-2α|wi|+α2))=min(∑(α2-2α|wi|+wi2))=min(nα2-2α∑|wi|+∑wi2)

可知当α=∑|wi|/n时函数取到min

代入化简得=-(∑|wi|)2/n+∑wi2

通分=(n∑wi2-(∑|wi|)2)/n

gc=gcd(n∑wi2-(∑|wi|)2,n)

所以p=(n∑wi2-(∑|wi|)2)/gc,q=n/gc

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3
 4 #define ll long long
 5 const int maxn=1e5+5;
 6 int a[maxn];
 7 int main()
 8 {
 9     int t,n;
10     scanf("%d",&t);
11     while(t--)
12     {
13         scanf("%d",&n);
14         ll sum=0,ac=0;
15         for(int i=1;i<=n;i++)
16         {
17             scanf("%d",&a[i]);
18             sum+=abs(a[i]);
19             ac+=a[i]*1LL*a[i];
20         }
21         ll gc=__gcd(ac*n-sum*sum,1LL*n);
22         printf("%lld/%lld\n",(ac*n-sum*sum)/gc,n/gc);
23     }
24     return 0;
25 }

原文地址:https://www.cnblogs.com/taozi1115402474/p/9824355.html

时间: 2024-11-13 08:06:00

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

hdu 5734 Acperience(2016多校第二场)

Acperience Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 484    Accepted Submission(s): 258 Problem Description Deep neural networks (DNN) have shown significant improvements in several applic

hdu 1719 Friend 数学推导

题链:http://acm.hdu.edu.cn/showproblem.php?pid=1719 Friend Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2099    Accepted Submission(s): 1058 Problem Description Friend number are defined recur

HDU 5734 Acperience

化简之后发现会是一个一元二次方程,对称轴大于0,所以把对称轴代入计算即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<ios

HDU 5734 Acperience (公式推导) 2016杭电多校联合第二场

题目:传送门. #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int gcd(long long a,long long b) { if(!b) return a; return gcd(b,a%b); } int a[100005]; int main() { int T,n; scanf("%d

hdu.5211.Mutiple(数学推导 &amp;&amp; 在logn的时间内求一个数的所有因子)

Mutiple Accepts: 476 Submissions: 1025 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 wld有一个序列a[1..n], 对于每个1≤i<n, 他希望你求出一个最小的j(以后用记号F(i)表示),满足i<j≤n, 使aj为ai的倍数(即aj mod ai=0),若不存在这样的j,那么此时令F(i) = 0 保证1≤n≤10000,1≤

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

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

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;

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 +