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

代码

 #include<bits/stdc++.h>
using namespace std; #define ll long long
const int maxn=1e5+;
int a[maxn];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ll sum=,ac=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=abs(a[i]);
ac+=a[i]*1LL*a[i];
}
ll gc=__gcd(ac*n-sum*sum,1LL*n);
printf("%lld/%lld\n",(ac*n-sum*sum)/gc,n/gc);
}
return ;
}

最新文章

  1. ThinkPHP动态版本控制
  2. DS实验题 融合软泥怪-2 Heap实现
  3. 利用spring boot创建java app
  4. 在windows编译MariaDB
  5. Android权限安全(2)给基本组件自定义权限(以activity为例)
  6. 编译器对C++ 11变参模板(Variadic Template)的函数包扩展实现的差异
  7. careercup-中等难度 17.3
  8. 关于Qt在子线程中使用QMessageBox的折衷方法
  9. Ext JS学习第四天 我们所熟悉的javascript(三)
  10. ASP.NET Core开发期间部署到IIS自定义主机域名并附加进程调试
  11. 利用mk-table-checksum监测Mysql主从数据一致性操作记录
  12. 在VS2017下配置OpenGL
  13. 字符流之FileReader&amp;FileWriter
  14. Linux安装nginx详细步骤
  15. Spring Boot(5)一个极简且完整的后台框架
  16. systemverilog soft constraint
  17. Java框架相关
  18. [SQLSERVER] [RESTORE] 逐步恢复日志备份并使用只读模式查看
  19. 配置idea
  20. iOS自学-UILabel常见属性

热门文章

  1. 01python语言程序设计基础——初识python
  2. jQeury 批量删除
  3. Excel技巧--巧用分列功能整理日期格式
  4. opencv::将两幅图像合并后,在同一个窗口显示;并将合并的图像流保存成视频文件
  5. SQL 中 CASE - WHEN - THEN - ELSE - END 的小结
  6. atop 分析小记
  7. 学习 MeteoInfo二次开发教程(一)
  8. VIVADO 入门之仿真与逻辑分析仪使用
  9. bitbucket 上公钥SSH key如何add key并进行项目运用
  10. Dubbo源码阅读顺序