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

Sample Output
/
/
/

题意:

  已知一种计算方式||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=b/2a。

  最后注意分子、分母分开计算,最后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==)
return a;
return gcd(b,a%b);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[];
long long sum1,sum;
sum1=sum=;//sum1为平方和,sum为和
for(int i=; i<n; i++)
{
cin>>a[i];
if(a[i]<)//一定取正值,因为b为-1或者+1,为了使最后结果最小,sum1应该最大
a[i]=-a[i];
sum1+=a[i];
sum+=(a[i]*a[i]);
}
/*接下来注意不能用b/2a 因为他不一定为整数,最后求得是分数,所以要将最后的公式化作分子分母形式*/
long long nb=n*sum-sum1*sum1 ;
long long x;
x=gcd(nb,n);
cout<<nb/x<<"/"<<n/x<<endl;
}
return ;
}

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

  1. hdu-5734 Acperience(数学)

    题目链接: Acperience Time Limit: 4000/2000 MS (Java/Others)   Memory Limit: 65536/65536 K (Java/Others) ...

  2. HDU 5734 Acperience(数学推导)

    Problem Description Deep neural networks (DNN) have shown significant improvements in several applic ...

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

    上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...

  4. 关于不同进制数之间转换的数学推导【Written By KillerLegend】

    关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...

  5. UVA - 10014 - Simple calculations (经典的数学推导题!!)

    UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  6. 『sumdiv 数学推导 分治』

    sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...

  7. LDA-线性判别分析(二)Two-classes 情形的数学推导

    本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...

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

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  9. [hdu5307] He is Flying [FFT+数学推导]

    题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...

  10. ZOJ3329(数学推导+期望递推)

    要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...

随机推荐

  1. C. Karen and Game

    C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  2. 再谈AbstractQueuedSynchronizer:独占模式

    关于AbstractQueuedSynchronizer JDK1.5之后引入了并发包java.util.concurrent,大大提高了Java程序的并发性能.关于java.util.concurr ...

  3. SSH连接不上CentOS 主机配置文件导致的原因的解决方法

    一.CentOS之SSH的安装与配置 SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定SSH 为建立在应用层和传输层基础上 ...

  4. Transparent Application Failover 透明应用切换

    官方文档地址 http://docs.oracle.com/cd/E11882_01/network.112/e41945/advcfg.htm#NETAG455 About Transparent ...

  5. jquery 封装

    (function($) { var plugName = "teamMingXi"; var teamMingXi = { open : function(type) { var ...

  6. SmartSql漫谈

    最近在看smartSql源码,兄弟写的.写的很不错取取经. 记录下一些学习的东西,刚开始我先不系统的写了,随意一点哈,我看的差不多再给大家一个模块一个模块系统的写. public T ExecuteS ...

  7. java udp服务器设计源码

    根据个人理解写的,轻喷. 没什么大的逻辑,直接上代码. UDPServer.java package mySocket;/* * 服务器端 */import java.awt.GridLayout;i ...

  8. day4作业小代码练习

    登录模块: 我们无论上那个网站,经常遇到这样的情况,让我们登录这个网站,流程图如下: 思路: 1.当我们登录网站的时候,我们首先会输入用户名,这个时候,有些网站会提醒我们用户名是否存在,如果我们输入的 ...

  9. SpringMvc多视图配置(jsp、velocity、freemarker) velocity在springmvc.xml配置VelocityViewResolver,VelocityConfigurer,FreeMarkerConfigurer,FreeMarkerViewResolver

    ?xml version="1.0"encoding="UTF-8"?> <beans xmlns="http://www.springf ...

  10. 算法起步之kmp算法

    [作者Idlear  博客:http://blog.csdn.net/idlear/article/details/19555905]            这估计是算法连载文章的最后几篇了,马上就要 ...